_this.subscribe不是角度2异步验证中的函数

时间:2016-10-18 21:32:42

标签: validation asynchronous angular

我尝试在角度2中进行异步验证但得到this.subscribe is not a function

我的FormGroup是:

const form = new FormGroup({
  example: new FormControl('example', [Validators.required], [CustomValidators.example]),
  last: new FormControl('Drew'),
});

我的自定义异步验证

....

example (value : number) : AsyncValidatorFn {
  return (control: AbstractControl): Observable< {[key:string]: boolean} > => {
    return this.restService.exampleService(value)  
}; 

exampleService以{match: true}

的形式返回一个对象
exampleService (value : any) {
  return this._http.get(AppSettings.API_ENDPOINT + "company/findByNit/" + value )
         .map(res => res.json());
}

1 个答案:

答案 0 :(得分:1)

验证应该只分组在一个数组中。试试这个

const form = new FormGroup({
  example: new FormControl('example', [Validators.required, CustomValidators.example]),
  last: new FormControl('Drew'),
});