我有一个异步验证器,它进行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();
}
}