I was able to create nested forms (w/o submit function) by following this code: https://plnkr.co/edit/vSODkb8i7o54X3fST9VF?p=preview
Now, I would like to submit the forms, however, having 2 types of errors:
- AppComponent.html:32 ERROR TypeError: Cannot read property 'emails' of undefined
- When saving, a new line is created, but the corresponding input is not.
I've created the stackbliz: https://stackblitz.com/edit/angular-tqrest
app.component.ts
constructor(private fb: FormBuilder,
private contractService: ContractService) {
}
ngOnInit() {
this.contractService.getContracts().subscribe(contracts => {
this.contracts = contracts;
});
this.trustForm = this.fb.group({
contracts: this.fb.array([])
});
this.addContract();
}
initContract() {
return this.fb.group({
name: '',
emails: this.fb.array([])
});
}
addContract() {
const contractArray = <FormArray>this.trustForm.controls['contracts'];
const newContract = this.initContract();
contractArray.push(newContract);
}
removeContract(idx: number) {
const contractsArray = <FormArray>this.trustForm.controls['contracts'];
contractsArray.removeAt(idx);
}
onSubmit(contract: Contract) {
this.contractService.addContract(contract);
}
How can I solve this?