在angular2中形成异步验证

时间:2017-01-29 14:07:33

标签: javascript angular typescript

我对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

可能出现什么问题

1 个答案:

答案 0 :(得分:1)

如果A = reshape(A,N,K*i); B = mat2cell(B,K,M*ones(1,i)); B{1} = sparse(B{1}); % make one sparse to trigger blkdiag into sparse mode C = A*blkdiag(B{:}); 指向thisValidationService需要

bind()