I am using Angular 8 with reactive forms.
On ngOnInit i am calling generateForm and patchDatainForm function simultaneously, and if patchData contain value then i want to push values in formArray.
constructor(private fb: FormBuilder) {
this.dataCollectionForm = this.fb.group({
items: this.fb.array([])
});
}
ngOnInit() {
this.generateForm();
this.patchDatainForm();
}
createBatchForm(line, type) {
return this.fb.group({
makeLineId: [type.makeLineId],
processTechType: [type.processTechType],
avgBct: ['', [Validators.required]],
bestBct: ['', [Validators.required]],
avgCapacity: ['']
});
}
itemTypes(): FormArray {
return this.dataCollectionForm.get("items") as FormArray;
}
generateForm() {
this.itemTypes().push(this.createBatchForm('Red', 'Batch'));
}
I have below data from backend to patch the arrays. I want to patch types array in my form fields.
patchDatainForm() {
const patchData = [{
makeLineName: 'Red',
types: [{
makeLineId: 6,
processTechType: "batch",
avgBct: 50,
bestBct: 200,
avgCapacity: 40
},
{
makeLineId: 5,
processTechType: "batch",
avgBct: 11,
bestBct: 10,
avgCapacity: 10
}]
}
];
this.dataCollectionForm.patchValue(patchData);
}
I commonly patch the values with this method this.dataCollectionForm.patchValue(patchData);, but its not working for dynamic form array.
Any help is appreciated. Thanks in Advance