I have a form with array values. Values are displaying in the form is perfectly but at the time of saving the value it is not binding the whole array values; instead of it is binding only last changed value.
Also, the formControlName="id" property value is not binding into formArray object.
App.component.ts
initializRecords(data?: any) {
this.dataForm = this.formBuilder.group({
dataCollection: this.formBuilder.group({
id: new FormControl(),
sizeId: new FormControl(),
sizes: this.formBuilder.group({
qty: new FormControl([])
})
})
});
}
App.component.html
<form [formGroup]="dataForm">
<div class="container">
<div class="row">
<div class="col">
<button mat-raised-button color="primary" (click)="saveRecords()">Save</button>
</div>
</div>
<div formArrayName="dataCollection">
<div class="row" *ngFor="let element of data; let first = first">
<div *ngIf="!first">----------</div>
<ng-container *ngFor="let subElement of element.sizes">
<div class="col">
<mat-form-field class="">
<input matInput type="text" formControlName="id" value={{element.value}} readonly>
</mat-form-field>
<mat-form-field class="">
<input matInput type="text" value={{subElement.subValue}} readonly>
<input type="hidden" formControlName="sizeId">
</mat-form-field>
<div formGroupName="sizes">
<mat-form-field class="">
<input matInput type="text" formControlName="qty" value={{subElement.qty}}>
</mat-form-field>
</div>
</div>
</ng-container>
</div>
</div>
</div>
</form>
=== Updated ===
On click of save event I would like the final JSON like this below structure as the values are displayed per row.
{
"data": [
{ "id": 1, "sizeId": 1, "qty": 10 },
{ "id": 1, "sizeId": 2, "qty": 1 },
{ "id": 1, "sizeId": 3, "qty": 1 },
{ "id": 2, "sizeId": 1, "qty": 50 },
{ "id": 2, "sizeId": 2, "qty": 60 },
{ "id": 2, "sizeId": 3, "qty": 70 },
]}