I need to have a formControl for two different arrays under the same node so I could add and delete a respective nested node in the given Reactive form.
Expected JSON Format:
{
"title": null,
"days": [
{
"date": null,
"nodes": [
{
"type": "leg",
"from": null,
"to": null
},
{
"type": "activity",
"category": null
}
]
}
]
}
I have implemented control on day and node for which I was able to add and delete the form fields respectively however I need separate controls for type leg and activity under nodes.
Is there any way to implement nested formControl in here?
Update: Need two formgroups under a form array, please help to implement in HTML.
initX() {
return this.fb.group({
'date': [],
'nodes': this.fb.array([
this.initY(),
this.initZ()
]),
});
}
initY() {
return this.fb.group({
'type': ['leg'],
'from': [],
'to': []
})
}
initZ() {
return this.fb.group({
'type': ['activity'],
'category':[],
'cost':[]
})
}
Error: Cannot find control with path: 'days -> 0 -> nodes -> 1 -> from'
Can find the stackblitz of the sample code here: https://stackblitz.com/edit/angular-ivy-fkgxrr
well, in my another answer, the "nodes" array is like
A formArray need has all his elements with the sames properties
Question: nodes is only an array with two elements (one for control Leg and another one for control category)?
In this case days is a formArray, each element is a formGroup with two peoperies, one of them is an array (not a formArray) with two elements
in this case you can has three getter
And use directly
[formGroup]
in a div, e.g.else I can not understand your json