I'm having a bit of an issue defining an array in reactive forms model: (since reactive froms can't simply accept a model (afaik) and work by it, I need to manually define it):
Object from Backend:
{
propOne: 'X',
propTwo: 'Y',
propArray: [
{
objPropA: 'a',
objPropB: 'b'
},
{
objPropA: 'c',
objPropB: 'd'
},
{
objPropA: 'e',
objPropB: 'f'
}
]
}
Component:
ngOnInit() {
this.myForm = this.fb.group({
propOne: '',
propTwo: '',
propArray: this.fb.array([this.createItem(), this.createItem(), this.createItem()])
});
// apply the value to the form
this.myForm.setValue(this.valueFromBackend);
}
private createItem(): FormGroup {
return this.fb.group({
objPropA: '',
objPropB: '',
});
}
I would like to avoid creating a form group for each element i'm expecting (this.createItem() x ElementsCount) when defining the form.
Am I really required to define how many elements i'm expecting in the my array?
Am I doing it wrong ?