运行代码时出现此错误:无法读取未定义的属性“getClientEmails”。 如果我从ngOnInit调用'clientService',而不是从'shouldBeUnique'方法中调用
,它就可以工作export class ClientFormComponent {
emails: any = {};
form = new FormGroup({
email: new FormControl('',
null,
this.shouldBeUnique),
password: new FormControl('', Validators.required),
confirm: new FormControl('', Validators.required),
firstname: new FormControl('', Validators.required),
lastname: new FormControl('', Validators.required),
address: new FormControl('', Validators.required),
phone: new FormControl('', Validators.required),
medical: new FormControl('', Validators.required)
});
constructor(public clientService: ClientService) { }
shouldBeUnique(control: AbstractControl): Promise<ValidationErrors | null> {
this.clientService.getClientEmails().subscribe(email => {
this.emails = email;
console.log(this.emails);
});
return new Promise((resolve, reject) => {
if (control.value === '')
resolve({ shouldBeUnique: true });
else
resolve(null);
});
}
}
答案 0 :(得分:1)
替换
this.shouldBeUnique
通过
(control: AbstractControl) => this.shouldBeUnique(control)