异步验证器Angular 2服务未定义?错误?

时间:2016-09-26 20:43:27

标签: validation asynchronous angular

我从Angular2异步验证器调用后端时遇到问题。我正在使用" final" Angular2的版本。

var intersect = svg.checkIntersection(this,rect.getBBox())

}

constructor(private formBuilder: FormBuilder,
          private profileService: ProfileService,
          private authService: AuthService,
          private router: Router) {

}

ngOnInit() {
this.registerForm = this.formBuilder.group({
username:  ['', [Validators.required, Validators.pattern('^[a-zA-Z0-9_]*$'), Validators.minLength(6),
  Validators.maxLength(20)], this.validateUsername],
email: ['', [Validators.required, this.validateEmail, Validators.maxLength(30)]],
password: ['', [Validators.required, Validators.pattern('^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*)(?=.*[!#$%&? "]).*$')]],
confirmPassword: ['', [Validators.required]]},
    {
      validator: this.matchingPasswords('password', 'confirmPassword')
    });

服务被初始化,即使我在validateUsername函数中调用它,它也适用于console.log()。

错误是: 无法读取属性"个人资料服务"来自undefined。

这是一个错误吗?或者我搞砸了什么?几个小时来看这个。

服务代码:

  validateUsername(control: FormControl): {[key: string]: any} {
    return new Promise((resolve, reject) => {
      this.profileService.isUsernameAvailable(control.value).subscribe(res => {
        if (res) {
          resolve(null);
        } else {
          resolve({ validateUsername: {valid: false} });
        }
      });
    });
  }

编辑:

   isUsernameAvailable(username: string): Observable<string> {
   return this.http.get('user?name=').map(res => res.json()).catch(this.handleError);;
  }

完整错误消息:

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {

0 个答案:

没有答案