I am using Angular 2 (TypeScript).
I want to do something for new selection, but what I got in onChange() is always last selection. How can I get new selection?
<select [(ngModel)]="selectedDevice" (change)="onChange($event)">
<option *ngFor="#i of devices">{{i}}</option>
</select>
onChange($event) {
console.log(this.selectedDevice);
// I want to do something here for new selectedDevice, but what I
// got here is always last selection, not the one I just select.
}
Just use [ngValue] instead of [value]!!
Another option is to store the object in value as a string:
component:
This was the only way I could get two way binding working to set the select value on page load. This was because my list that populates the select box was not the exact same object as my select was bound to and it needs to be the same object, not just same property values.
In Angular 5 I did with the following way. get the object $event.value instead of $event.target.value