如果密码或电子邮件中的任何一个无效,我正在运行登录表单以显示错误 到目前为止,这就是我所拥有的
<form [formGroup]="loginForm" (ngSubmit)="SignIn(email.value, password.value)">
<mat-form-field>
<input formControlName="email" name="email" matInput type="text" placeholder="email">
</mat-form-field>
<mat-form-field>
<input formControlName="password" name="password" matInput type="password" placeholder="Password">
</mat-form-field>
<div *ngIf="errorCode" class="notification is-danger">
Email and password does not match
</div>
<div class="d-flex flex-column align-items-center">
<button mat-raised-button class="button is-success w-100" type="submit" [disabled]="!loginForm.valid">
Continue
</button>
</div>
</form>
和我的ts
ngOnInit() {
this.loginForm = this.fb.group({
email: ['', [
Validators.required,
Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$')
]],
password: ['',
Validators.required
],
});
}
SignIn(email, password) {
return this.afAuth.auth.signInWithEmailAndPassword(email, password)
.then((result) => {
this.ngZone.run(() => {
this.router.navigate(['dashboard']);
});
}).catch((error) => {
this.errorCode= error.message;
console.log(this.errorCode)
})
}
但是,当我提交了无效的用户时,似乎未显示错误消息。 以前我已经用
进行了测试<div *ngIf="!loginForm.valid" class="notification is-danger">
Email and password does not match
</div>
但是问题在于它仅根据字段的填充位置显示,而不能验证字段本身。
答案 0 :(得分:0)
这是一个很长的路要走,但是,您是将错误分配给组件,通常只有在组件定义中声明了changeDetection: ChangeDetectionStategy.OnPush
时,该错误才会失败。如果是这样,则只需注入变更检测器引用constructor(private cd: ChangeDetectorRef) {}
,并在.catch
块中分配错误值后使用它,就像这样:
}).catch((error) => {
this.errorCode= error.message;
console.log(this.errorCode)
this.cd.detectChanges();
})