【发布时间】:2017-03-14 16:39:43
【问题描述】:
我有一个 Angular 2 应用程序,它使用 ReactiveForms 模块来管理使用自定义验证器的表单。验证器收到一个FormControl 对象。当FormControl 被传递给验证器时,我有几个输入字段可以使用相同的自定义验证器。
我在FormControl 上找不到任何公开输入字段名称的方法或公共属性。当然,这很简单,可以看出它的价值。以下显示了我想如何使用它:
public asyncValidator(control: FormControl): {[key: string]: any} {
var theFieldName = control.someMethodOfGettingTheName(); // this is the missing piece
return new Promise(resolve => {
this.myService.getValidation(theFieldName, control.value)
.subscribe(
data => {
console.log('Validation success:', data);
resolve(null);
},
err => {
console.log('Validation failure:', err);
resolve(err._body);
});
});
}
【问题讨论】:
标签: angular typescript angular2-forms angular-forms