综合编程

How do I get a new selection in & ldquo; Select & rdquo; In angular 2?

微信扫一扫,分享到朋友圈

How do I get a new selection in & ldquo; Select & rdquo; In angular 2?
0 0

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?

    {{i}}


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:

    {{i}}


onChange(deviceValue) {
    console.log(deviceValue);
}

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

    {{i}}
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
:

  {{i.name}}

{{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



Plunker
- uses


and uses the new forms API

Hello, buddy!
感谢您的支持!

    IronWolf Pro 14TB Hard Drive review: Seagate's best gets more capacity and speed

    上一篇

    Conversion of the date of the string with the offset to the different time zone

    下一篇

    您也可能喜欢

    评论已经被关闭。

    插入图片