Trying to insert server data into Angular's form so that the FormGroup property key is that from the server data. Followed Angular's docs on dynamic forms but it wasn't exactly what i wanted (https://angular.io/guide/reactive-forms#creating-dynamic-forms).
The point is to populate the form with the data and display it in template with *ngFor using sliders on each property to get a value. Then sending the data back to server.
Example of the data object:
{
"things": [
{
"thingId": 1,
"thingName": "bear"
},
{
"thingId": 2,
"thingName": "piano"
}
]
}
Service getting the data and assigning to variable:
things: IThing[] = [];
getThings() {
this.thingService.getThings()
.subscribe(data => {
this.things = data.things;
});
}
I need to populate the things FormGroup (or FormArray if needed) with the values from the data object fetched from the server, that is, the thingName property value => "bear", "piano".
ngOnInit() {
this.myForm = this.fb.group({
title: ['', []],
comment: ['', []],
// need to populate the things object here, example:
things: this.fb.group([
// bear: [,[]],
// piano: [,[]]
// and so on...
])
})
this.getThings();
}