【问题标题】:angular 8 how to pass parent class value to child class角度8如何将父类值传递给子类
【发布时间】:2021-03-14 11:08:41
【问题描述】:
我想为buyer.cnic、buyer.name 和buyer.phone 分配价值数据。
我得到的值是 {sCommercial: true
玛拉:“12”
总金额:“0121233123”
}
export class AddDhaFile {
buyer: Buyer[];
dealNumber?: string;
marla: number;
isCommercial: boolean;
totalAmount: number;
payments?: Payment[];
constructor(value: any) {
this.marla = value.marla;
this.isCommercial = value.isCommercial;
this.totalAmount = value.totalAmount;
}
}
export class Buyer {
name: string;
cnic: string;
phone: string;
constructor(value: any) {
this.name = value.name;
this.cnic = value.cnic;
this.phone = value.phone;
}
}
【问题讨论】:
标签:
angular
typescript
interface
【解决方案2】:
我假设值对象看起来像这样。
value = {
isCommercial: true,
marla: "12",
totalAmount: "0121233123",
buyer: {name: 'xyx' , cnic: 'some digits', phone:'0900 78601'}
}
export class AddDhaFile {
buyer: Buyer[];
dealNumber?: string;
marla: number;
isCommercial: boolean;
totalAmount: number;
payments?: Payment[];
constructor(value: any) {
this.marla = value.marla;
this.isCommercial = value.isCommercial;
this.totalAmount = value.totalAmount;
this.buyer.append(new Buyer(value.buyer))
}
}
或者如果值对象看起来像这样。
value = {
isCommercial: true,
marla: "12",
totalAmount: "0121233123",
buyers: [{...}, {...}, {...}]
}
export class AddDhaFile {
buyer: Buyer[];
dealNumber?: string;
marla: number;
isCommercial: boolean;
totalAmount: number;
payments?: Payment[];
constructor(value: any) {
this.marla = value.marla;
this.isCommercial = value.isCommercial;
this.totalAmount = value.totalAmount;
this.buyer = value.buyers.map(buyer => new Buyer(buyer)))
}
}