I am new to angular, currently using Angular v.8. I have a json map in my component. I want to use this map to create my input fields (key as field label and value as the input value) in html. I have iterated this map and populated the fields based on the json map data, but editing the value is not updating the actual json map. I need the updated map back in the component to send to backend API's. Please help, code snippets below:
component:
..
mapRecord: any = {"Col1":"Val1", "Col2":"Val2", "Col3":""}
...
html:
<table>
<tr *ngFor="let record of mapRecord | keyvalue">
<td>
<div [formGroup]="addGlimpseForm">
<mat-form-field> <input matInput
placeholder="{{record.key}}" [(ngModel)]="record.value"
formControlName="{{record.key}}" name="record.value" id="record.value">
</mat-form-field>
</div>
</td>
</tr>
</table>
{{ mapRecord | json }}
EDIT: I already have the form control populated dynamically at component:
glimpseFields: string[] = [];
..
for (var key in this.mapRecord) {
this.glimpseFields.push(key);
}
this.addGlimpseForm = new FormGroup({username: new FormControl('', [])});
for (let glimpseField of this.glimpseFields) {
this.addGlimpseForm.addControl(glimpseField, new FormControl('', Validators.required));
}
[formGroup]="addGlimpseForm"which indicates a reactive form and[(ngModel)]which indicates a template driven form. Don't mix.