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.
}
If you don't need two-way data-binding:
For two-way data-binding, separate the event and property bindings:
If
devices
is array of objects, bind tongValue
instead ofvalue
:Plunker - does not use
<form>
Plunker - uses
<form>
and uses the new forms APII used this for angular Material dropdown. works fine
You can pass the value back into the component by creating a reference variable on the select tag
#device
and passing it into the change handleronChange($event, device.value)
should have the new valueI ran into this problem while doing the Angular 2 forms tutorial (TypeScript version) at https://angular.io/docs/ts/latest/guide/forms.html
The select/option block wasn't allowing the value of the selection to be changed by selecting one of the options.
Doing what Mark Rajcok suggested worked, although I'm wondering if there's something I missed in the original tutorial or if there was an update. In any case, adding
to hero-form.component.ts in the HeroFormComponent class
and
to hero-form.component.html in the
<select>
element made it workuse selectionChange in angular 6 and above. example
(selectionChange)= onChange($event.value)
latest ionic 3.2.0 have modified (change) to (ionChange)
eg: HTML
TS