我正在使用 Angular 2(TypeScript)。
我想对新选择做点什么,但我在 onChange()
中得到的始终是最后一个选择。我怎样才能获得新的选择?
<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.
}
原文由 Hongbo Miao 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果您不需要双向数据绑定:
对于双向数据绑定,将事件绑定和属性绑定分开:
如果
devices
是对象数组,则绑定到ngValue
而不是value
:Plunker
- 不使用<form>
Plunker
- 使用<form>
并使用新的表单 API