我很难将一个有角度的对象链接到另一个。
我有一个目标公司,它有一个财产账户,这是一个FiAccounts数组。
export class Company {
id : number;
name : String;
country : String;
accounts : FiAccount[];
}
export class FiAccount {
id:number;
year : number;
equity : number;
long_term_debt : number;
assets : number;
}
在我的表单中,我创建了一个newco和一个newfi,效果很好。但是,我无法在我的newco中绑定newfi。当我尝试使用:
this.newco.accounts.push(this.newfi);
我在控制台中有错误:
"错误类型错误:this.newco.accounts未定义">
这是我的TS文件的代码。你能解释一下我在哪里犯的错吗?
export class NewcoComponent implements OnInit {
newco : Company = new Company();
newfi : FiAccount = new FiAccount();
constructor(private service : CompanyService, private accountService : AccountsService) { }
ngOnInit(): void {
}
onSubmit(){
this.service.postCompany(this.newco).subscribe(() =>{
});
this.accountService.postAccount(this.newfi).subscribe(()=>{
});
this.newco.accounts.push(this.newfi);
}
创建新的Company
时,accounts
成员未定义。
您需要将其初始化为一个空数组:
export class Company {
id : number;
name : String;
country : String;
accounts : FiAccount[] = [];
}
演示:https://stackblitz.com/edit/so-company-accounts?file=index.ts