当我在RegisterForm中输入电子邮件时,它等于将['emailTaken']设置为true的电子邮件。我收到以下错误代码:
ERROR TypeError: Cannot read property 'emailTaken' of null
这是我函数的代码:
checkEmail(control:FormControl): Promise<any> | Observable<any>{
const answer = new Promise<any>((resolve, reject) => {
setTimeout(() => {
if(control.value == 'stijn.wauters@outlook.com'){
resolve({'emailTaken': true})
}else{
resolve(null)
}
},1500);
});
return answer;
}
在这里,我触发HTML中的错误:
<span *ngIf="registerForm.get('email').errors['emailTaken']" class="help-block">
Email is already taken!
</span>
如何解决此问题,以免出现错误?
答案 0 :(得分:1)
您可以通过选中null。运算符
<span *ngIf="registerForm.get('email')?.errors['emailTaken']" class="help-block">
Email is already taken!
</span>
答案 1 :(得分:1)
hasError对于检查错误可能更有用。
<span *ngIf="registerForm.get('email').hasError('emailTaken')" class="help-block">
Email is already taken!
</span>