I'm trying to use *ngFor
to build a bundle of multiple <select>
(3x of them) and I found this other SO to get me going. However, the problem I have now is that they are not independent, if I change the first select, it also reflects the change on the second select (but not the third one??). One thing worth to know is that all Select have the same list, which is why the first one affects second one, but I really want them independent.
Oh I'm currently using Angular 5, so it's not the latest.
Here's the code I have to loop through the 3 Select with *ngFor
<div class="col-sm-3" *ngFor="let groupField of selectedGroupingFields; let i = index;">
<select class="form-control col-sm-6" name="groupField{{i}}" [(ngModel)]="selectedGroupingFields[i]" (ngModelChange)="groupByFieldName($event, i)">
<option value=""></option>
<option [ngValue]="field.id" *ngFor="let field of columnDefinitions">{{field.name}}</option>
</select>
With the collection being structure like this
selectedGroupingFields: string[] = ['', '', ''];
columnDefinitions = [
{ id: 'title', name: 'Title'},
{ id: 'duration', name: 'Duration'},
{ id: 'percentComplete', name: '% Complete'},
{ id: 'start', name: 'Start'},
{ id: 'Finish', name: 'Finish'},
{ id: 'cost', name: 'Cost'},
{ id: 'effortDriven', name: 'Effort-Driven'}
];
To demo the problem, I made this little function that is triggered by a button click
changeFirstGroupBy() {
this.selectedGroupingFields[0] = 'title';
}
When I click my button and execute that function, it changes the first 2 Select... why?
So I did more tests and decided to create each Select separately by recopying them 1 by 1 and just remove the *ngFor
on the <div>
like this
<div class="col-sm-3">
<select class="form-control col-sm-6" name="groupField1" [(ngModel)]="selectedGroupingFields[0]" (ngModelChange)="groupByFieldName($event, i)">
<option value=""></option>
<option [ngValue]="field.id" *ngFor="let field of columnDefinitions">{{field.name}}</option>
</select>
</div>
<div class="col-sm-3">
<select class="form-control col-sm-6" name="groupField2" [(ngModel)]="selectedGroupingFields[1]" (ngModelChange)="groupByFieldName($event, i)">
<option value=""></option>
<option [ngValue]="field.id" *ngFor="let field of columnDefinitions">{{field.name}}</option>
</select>
</div>
<div class="col-sm-3">
<select class="form-control col-sm-6" name="groupField3" [(ngModel)]="selectedGroupingFields[2]" (ngModelChange)="groupByFieldName($event, i)">
<option value=""></option>
<option [ngValue]="field.id" *ngFor="let field of columnDefinitions">{{field.name}}</option>
</select>
</div>
and that works! but why? That is supposed to be the exact same code as the *ngFor
. Also if I change the last select (see in demo), it affects the second select too, that is so strange but it's really working correctly when I create all 3 Select manually.
Here's an animated GIF of the problem
EDIT
Forgot to mention, I also tried to change 2 way binding to 1 way and it has the same effect [ngModel]="selectedGroupingFields[i]"
ANSWERED
This is now working and used for an Open Source library of mine Angular-Slickgrid and here's the demo of where I use this answer. Thanks :)