Skip to content
Advertisement

How do I detect change to ngModel on a select tag (Angular 2)?

I am attempting to detect a change on ngModel in a <select> tag. In Angular 1.x, we might solve this with a $watch on ngModel, or by using ngChange, but I’ve yet to understand how to detect a change to ngModel in Angular 2.

Full Example: http://plnkr.co/edit/9c9oKH1tjDDb67zdKmr9?p=info

JavaScript

As we can see, if we select a different value from the dropdown, our ngModel changes, and the interpolated expression in the view reflects this.

How do I get notified of this change in my class/controller?

Advertisement

Answer

Update:

Separate the event and property bindings:

JavaScript
JavaScript

You could also use

JavaScript

and then you wouldn’t have to update the model in the event handler, but I believe this causes two events to fire, so it is probably less efficient.


Old answer, before they fixed a bug in beta.1:

Create a local template variable and attach a (change) event:

JavaScript

plunker

See also How can I get new selection in “select” in Angular 2?

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement