我试图检测<select>
标签中ngModel
的变化.在1.x、 我们可以用$watch
对ngModel
或者用ngChange
来解决这个问题,但我还不知道如何在Angular 2中检测到ngModel
的变化.
Full Example: http://plnkr.co/edit/9c9oKH1tjDDb67zdKmr9?p=info
import {Component, View, Input, } from 'angular2/core';
import {FORM_DIRECTIVES} from 'angular2/common';
@Component({
selector: 'my-dropdown'
})
@View({
directives: [FORM_DIRECTIVES],
template: `
<select [ngModel]="selection" (ngModelChange)="onChange($event, selection)" >
<option *ngFor="#option of options">{{option}}</option>
</select>
{{selection}}
`
})
export class MyDropdown {
@Input() options;
selection = 'Dog';
ngOnInit() {
console.log('These were the options passed in: ' + this.options);
}
onChange(event) {
if (this.selection === event) return;
this.selection = event;
console.log(this.selection);
}
}
正如我们可以看到的,如果我们从下拉列表中 Select 一个不同的值,我们的ngModel
将发生变化,视图中的插值表达式反映了这一点.
我如何得知班级/管制员的变化?