使用ng模型变量的角度2中的ng变化 [英] using the ng change in angular 2 using ng model variable
本文介绍了使用ng模型变量的角度2中的ng变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在角度2中使用ng-change事件?每当ng-model变量更改时,都必须调用一个函数.
How can I use the ng-change event in angular 2? Whenever the ng-model variable is changed, a function has to be called.
[(ngModel)]="variable"
ngchange=variable;
推荐答案
您可以使用ngModelChange
事件:
[(ngModel)]="variable" (ngModelChange)="doSomething($event)"
修改
根据您的评论,我认为您应该使用带有自定义验证程序的表单控件.
According to your comment, I think that you should use form control with a custom validator.
以下是示例:
@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
});
}
}
有关更多详细信息,请参见本文:
See this article for more details:
这篇关于使用ng模型变量的角度2中的ng变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文