I want to add dynamic form array controls with the values according to response coming from API. If there 1 record then 1 row if 2 then 2 rows and so on.
Form:
this.VendorForm = this.formBuilder.group({
vendorId: [null],
vendorName: ['', [Validators.pattern(/^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$/), Validators.required]],
vendorType: ['', Validators.required],
correspondingAddressDetail: this.formBuilder.group({
correspondingAddress: ['', Validators.required],
correspondingState: ['', [Validators.required, Validators.pattern(/^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$/)]],
correspondingPincode: ['', [Validators.required, Validators.pattern(/^\d{3}\d{3}$/)]],
correspondingCity: ['', Validators.required],
correspondingDistrict: ['', Validators.required],
correspondingPostoffice: ['', Validators.required]
}),
billingAddressDetail: this.formBuilder.group({
sameAddress: [false],
billingAddress: ['', Validators.required],
billingState: ['', [Validators.required, Validators.pattern(/^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$/)]],
billingPincode: ['', [Validators.required, Validators.pattern(/^\d{3}\d{3}$/)]],
billingCity: ['', Validators.required],
billingDistrict: ['', Validators.required],
billingPostoffice: ['', Validators.required]
}),
phoneNo: ['', [Validators.required, Validators.pattern(/^[1-9]\d{9}$/)]],
email: ['', Validators.pattern(/^(\D)+(\w)*((\.(\w)+)?)+@(\D)+(\w)*((\.(\D)+(\w)*)+)?(\.)[a-z]{2,}$/)],
contactPersonNo: ['', [Validators.required, Validators.pattern(/^[1-9]\d{9}$/)]],
contactPersonName: ['', [Validators.required, Validators.pattern(/^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$/)]],
contactPersonEmail: ['', Validators.pattern(/^(\D)+(\w)*((\.(\w)+)?)+@(\D)+(\w)*((\.(\D)+(\w)*)+)?(\.)[a-z]{2,}$/)],
statutoryInfo: this.formBuilder.array([this.formBuilder.group({
statutoryName: ['', Validators.required],
info: ['', Validators.required]
})]),
isActive: [true],
remarks: ['']
});
patching value:
this.dialogData.statutoryInfo.forEach(x => {
this.statutoryInfo.push(this.formBuilder.group({
statutoryName: [x.statutoryName],
info: [x.info]
}))
});
value of statutoryInfo coming from API response:
(2)[{…}, {…}]
0:{statutoryName: 6, info: '123456987'}
info:'123456987'
statutoryName:6
1:{statutoryName: 2, info: 'BnQpgw444'}
info:'BnQpgw444'
statutoryName:2
after 1st click on Edit button:(throws error and the rows did not get inserted)

after 2nd click on Edit button:(did not throw error and the rows did get inserted)

I don't understand why after the second click the values got inserted. and I want to remove the empty row of formArray controls.