我是 angular2 新手,我想验证服务器的电子邮件地址,但失败了
这是我的表格
this.userform = this._formbuilder.group({
email: ['', [Validators.required], [this._validationService.emailExistsValidator.bind(this)]],
});
然后我有 _ValidationService 验证服务是带有@Injector的服务
@Injectable()
export class ValidationService{
constructor(
private _authService:AuthService
){}
emailExistsValidator(control) {
if (control.value != undefined) {
return this._authService.checkExists("email")
.map(response => {
if (!response) {
return {'emailNotExists': true};
}
});
}
}
}
在我的 authservice 中(这是执行 http 请求的另一个服务)
@Injectable()
export class AuthService {
checkExists(value):Observable<any>{
return this._http.get(this.authurl+value)
.map(response => {
return response //this is either true or false
});
}
}
我已关注This link 在设置我的表单但失败了
返回的错误是
Cannot read property 'checkExists' of undefined
at UsersComponent.webpackJsonp.187.
ValidationService.emailExistsValidator
可能出了什么问题
最佳答案
如果this
应该指向ValidationService
,则需要bind()
this.userform = this._formbuilder.group({
email: ['', [Validators.required], [this._validationService.emailExistsValidator.bind(this._validationService)]],
});
关于javascript - Angular2 中的表单异步验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41921768/