运行代码时出现此错误:无法读取未定义的属性“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);
});
}
}
代替
this.shouldBeUnique
经过
(control: AbstractControl) => this.shouldBeUnique(control)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句