0

I would like to have something similar to this:

<select>
  <option [bound]="items[i]" *ngFor="let item of items; #i = index">{{item}}</option>
</select>
cfischer
  • 470
  • 8
  • 24

3 Answers3

1

I now use:

<select [(ngModel)]="selectedItem">
  <option [ngValue]="item" *ngFor="let item of items">{{item}}</option>
</select>

You can use:

<select [(ngModel)]="selectedItem" (ngModelChange)="onItemSelected(selectedItem)">
  <option [ngValue]="item" *ngFor="let item of items">{{item}}</option>
</select>

to detect changes.

[ngValue] allows to bind complex objects, see here.

Community
  • 1
  • 1
cfischer
  • 470
  • 8
  • 24
0

I think you should bind item or i to [value]. Also # is prohibited. and you're lacking of [(ngModel)] on your <select>.

Take a look at Forms in angular docs, especially here: Add Powers with *ngFor.

mat3e
  • 781
  • 5
  • 17
0

I Would use something like this:

<select [(ngModel)]="currValue" >
    <option *ngFor="let item of items" [value]="item.value"> {{ item.name }} </option>
</select>

With items being an array of objects(value, name).

Daniel Pliscki
  • 1,913
  • 16
  • 24