my API response is like
"groups": [{
"group_name": "GRP1"
"attributes": [{
"attribute_id": 1,
"attribute_name": "Frequency",
"value_reference": "tag"
}]
},
{
"group_name": "GRP2"
"attributes": [{
"attribute_id": 2,
"attribute_name": "Date",
"value_reference": "static",
"value_static_type": "date"
}]
}]
-- How I can create formConrol in Angular 4 to display data like
GroupName
List of Group's Attribute
GroupName
List of Group's Attribute
My initial form control is like
this.editTemplateForm = this.fb.group({
groups: this.fb.group({
group_name: ['', Validators.required ],
attributes : this.fb.group({
value_reference : []
})
}),
});
I don't understand how add control dynamically
The structure here ends up being fairly complicated, if you'd like to match your API response exactly. Since, so far, each
attributes
property only has a single object of attributes, you could potentially makeattributes
an Object directly, instead of an array of objects, which would simplify the following code. The code below matches your current API structure, and can be played with at this working plunker.Some things to keep in mind:
FormGroup
needs to be the outer-most form object, and can also be used to hold an indeterminate number ofFormControl
itemsFormArray
, on the other hand, is useful when the number of controls is indeterminate, and it does not matter what those controls are called.allGroups
is an array holding the largest groups, andattributes
is an array holding your attribute objects, but those attribute objects themselves are groups - because we want to be able to name the controls based on their API property nameThere's also a good chance this structure isn't exactly what you're looking for (e.g. perhaps you didn't want all these values to be editable
<input>
fields), but it should give you a strong basis to play with on the plunker, and understand how you can dynamically generate HTML based on changing form objects.