Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get new selection in "select" in Angular 2?

I am using Angular 2 (TypeScript).

I want to do something with the new selection, but what I get in onChange() is always the last selection. How can I get the 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 with the new selectedDevice, but what I     // get here is always the last selection, not the one I just selected. } 
like image 489
Hongbo Miao Avatar asked Nov 13 '15 19:11

Hongbo Miao


People also ask

How do I get the selected value of dropdown in TypeScript?

First, create the template variables for dropdown using #teams . Use the event binding to listen for the change event and link with the onSelect method. The onSelect method receives the reference variable and takes the value of the dropdown element. Next, create the onSelected(value) method in the TypeScript file.

What the selector option does in angular?

What is a Selector in Angular? A selector is one of the properties of the object that we use along with the component configuration. A selector is used to identify each component uniquely into the component tree, and it also defines how the current component is represented in the HTML DOM.

What does means in angular?

Means safe navigation operator. From Docs. The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths.


2 Answers

If you don't need two-way data-binding:

<select (change)="onChange($event.target.value)">     <option *ngFor="let i of devices">{{i}}</option> </select>  onChange(deviceValue) {     console.log(deviceValue); } 

For two-way data-binding, separate the event and property bindings:

<select [ngModel]="selectedDevice" (ngModelChange)="onChange($event)" name="sel2">     <option [value]="i" *ngFor="let i of devices">{{i}}</option> </select> 
export class AppComponent {   devices = 'one two three'.split(' ');   selectedDevice = 'two';   onChange(newValue) {     console.log(newValue);     this.selectedDevice = newValue;     // ... do other stuff here ... } 

If devices is array of objects, bind to ngValue instead of value:

<select [ngModel]="selectedDeviceObj" (ngModelChange)="onChangeObj($event)" name="sel3">   <option [ngValue]="i" *ngFor="let i of deviceObjects">{{i.name}}</option> </select> {{selectedDeviceObj | json}} 
export class AppComponent {   deviceObjects = [{name: 1}, {name: 2}, {name: 3}];   selectedDeviceObj = this.deviceObjects[1];   onChangeObj(newObj) {     console.log(newObj);     this.selectedDeviceObj = newObj;     // ... do other stuff here ...   } } 

Plunker - does not use <form>
Plunker - uses <form> and uses the new forms API

like image 57
Mark Rajcok Avatar answered Sep 28 '22 05:09

Mark Rajcok


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 handler onChange($event, device.value) should have the new value

<select [(ng-model)]="selectedDevice" #device (change)="onChange($event, device.value)">     <option *ng-for="#i of devices">{{i}}</option> </select>  onChange($event, deviceValue) {     console.log(deviceValue); } 
like image 20
Brocco Avatar answered Sep 28 '22 06:09

Brocco