Currently, I am dynamically generating mat-inputs
using *ngFor
. My goal is to store each value of [(ngModel)]
in a separate array (not the one used in *ngFor
) based on the index of the *ngFor
elements. Here's my implementation:
<div *ngFor="let item of items; let id = index;">
<mat-form-field>
<mat-select [(ngModel)]="differentArray[id].first" (ngModelChange)="onSelection()">
<mat-option *ngFor="let number of arrayOfNumbers" [value]="number">{{number}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-select [(ngModel)]="differentArray[id].second" (ngModelChange)="onSelection()">
<mat-option *ngFor="let number of arrayOfNumbers" [value]="number">{{number}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-select [(ngModel)]="differentArray[id].third" (ngModelChange)="onSelection()">
<mat-option *ngFor="let number of arrayOfNumbers"[value]="number"> {{number}} </mat-option>
</mat-select>
</mat-form-field>
</div>
However, I've noticed that all the values for different *ngFor
loops end up being the same. Why is this happening and what can be done to properly differentiate them based on element id?