Reputation:
The issue can be seen in this stackblitz :
I have a payload :
payload = {
id: 1,
name: 'This is the payload',
children: [
{ id: 1, name: 'Child 1' }
],
};
I create a form group out of that :
this.form = this.fb.group(this.payload);
I then display the value of the form :
{{ form?.value | json }}
And the result is :
{ "id": 1, "name": "This is the payload", "children": { "id": 1, "name": "Child 1" } }
As you can see, the children
property went from an array, to an object.
Upvotes: 3
Views: 257
Reputation: 7713
You should declare an array of array, as group takes array as argument [value, ...validators] :
payload = {
id: 1,
name: 'This is the payload',
children: [[
{ id: 1, name: 'Child 1' },
]],
};
To declare an array value according to angular.io :
this.fb.group({
anArray: [[]]
});
// OR
this.fb.group({
anArray: this.fb.array([])
});
// OR
this.fb.group({
anArray: new FormControl([])
});
If you want to make dynamic forms, have a look at this angular.io page.
Upvotes: 5