如何在 Angular 2 中使用 ng-change 事件?每当 ng-model 变量发生变化时,就必须调用一个函数。
[(ngModel)]="variable"
ngchange=variable;
你可以使用ngModelChange
event:
[(ngModel)]="variable" (ngModelChange)="doSomething($event)"
Edit
根据您的评论,我认为您应该将表单控件与自定义验证器一起使用。
这是一个示例:
@Component({
(...)
template: `
<input [(ngModel)]="variable" [ngFormControl]="ctrl"/>
`
})
export class SomeComponent {
constructor() {
this.ctrl = new Control('', (control) => {
// validate the value
});
this.ctrl.valueChanges.subscribe((value) => {
// called when the value is updated
});
}
}
请参阅这篇文章了解更多详细信息:
- http://restlet.com/blog/2016/02/11/implementing-angular2-forms-beyond-basics-part-1/ http://restlet.com/blog/2016/02/11/implementing-angular2-forms-beyond-basics-part-1/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)