Angular Material电子邮件验证不适用于模板驱动的表单

时间:2018-10-06 22:32:37

标签: angular typescript angular-material angular6

您能告诉我为什么这不起作用吗? <mat-error *ngIf="email.errors.email">。但这很好。 <mat-error *ngIf="email.errors.required"> Email is required.

<form name="form" (ngSubmit)="f.form.valid && submit()" #f="ngForm" novalidate>
        <div class="form-group">
            <mat-form-field>
                <mat-label>Email</mat-label>
                <input matInput type="email" name="email" placeholder="Email address" autocomplete="off" [(ngModel)]="model.email"
                 #email="ngModel" email="true" required>
                <mat-error *ngIf="email.errors.email">
                    Email is invalid.
                </mat-error>
                <mat-error *ngIf="email.errors.required">
                    Email is required.
                </mat-error>
            </mat-form-field>
        </div>
    </form>

1 个答案:

答案 0 :(得分:0)

找到了解决方案。应该是这样。

注意:?是重要的部分。

<mat-error *ngIf="email.errors?.email && !email.errors?.required">
                Email is invalid.
</mat-error>