Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular input events not triggered with ngModel binding

I am currently working on a small angular project and have a problem with input events.

I have following code:

<select (change)="onSelect($event.target.value)" >   
    <option *ngFor="let option of options" [value]="option.id">{{option.name}}</option> 
</select>

This is working fine and well. But I wanted to change it to following:

<select (change)="onSelect($event.target.value)" [(ngModel)]="selectedItem">
    <option *ngFor="let option of options" [value]="option.id">{{option.name}}</option>
</select>

Now after this the change event is not triggered anymore. I also tried [ngModel] and (ngModelChange) and a lot more. But as long as there is something with ngModel and a binding in it the events do not trigger anymore.

All my Angular stuff is in version 5.1.2, the CLI in 1.6.2

Does anyone know why Angular has that behaviour? Greetings and thank you.

like image 336
nic Avatar asked Nov 16 '25 04:11

nic


1 Answers

it could work like this:

<select type="number" [(ngModel)]="selectedItem" >
  <option *ngFor="let option of options" [ngValue]="option.id">{{option.name}}</option>
</select>

Here is working plnkr hope it solve your issue bro :)

like image 116
Bassem Mamar Avatar answered Nov 18 '25 18:11

Bassem Mamar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!