角度反应式自定义异步验证器,处理http错误

时间:2018-02-23 17:18:18

标签: angular validation angular-reactive-forms

我有一个异步验证器,它进行http调用以进行后端验证。需要处理http调用失败,无法找到任何方法来捕获它。任何帮助确定正确的设置将不胜感激。以下是我目前的设置

表单控件

this.email = new FormControl('', {
  validators: [
    Validators.email,
    Validators.required,
    Validators.maxLength(100)
    )
  ],
  asyncValidators: this.checkEmail.bind(this),
  updateOn: 'blur'
});

我的异步验证器

checkEmail(control: AbstractControl) {
return this.emailService.checkEmail(control.value).map(res => {

    if (this.res.invalid) {
      return {email: true};
    }
   return null;    
});
}

服务层中的http调用

 public checkEmail(email: string): Observable<any> {
    if (emailAddress) {
     return this.http.get(environment.API_URL + 'register/checkEmail?email=' + emailAddress);
   } else {
     return empty();
   }
 }

0 个答案:

没有答案