【发布时间】:2019-05-06 19:49:56
【问题描述】:
我使用的是响应式表单,并且有多个输入字段,每个字段都有各自的 mat-error。所有输入字段错误消息都出现在输入框之外,这是正确的并且默认情况下是正确的。但是电子邮件输入字段是唯一的,它的错误消息出现在输入字段内。
这是代码
<!-- username -->
<div class="">
<mat-form-field class="">
<input type="email" matInput
placeholder="Email"
formControlName="username" required>
<div *ngIf="isFieldValid('username')">
<mat-error
*ngIf="modelForm.controls['username'].hasError('required')
&&
modelForm.get('username').touched">
This is required
</mat-error>
<mat-error
*ngIf="modelForm.controls['username'].hasError('pattern')">
Email invalid
</mat-error>
<mat-error
*ngIf="!modelForm.controls['username'].hasError('required')
&&
!modelForm.controls['username'].hasError('pattern')
&&
modelForm.controls['username'].hasError('isDupe')">
This email has been used already
</mat-error>
</div>
</mat-form-field>
</div>
<!-- password -->
<div class="">
<mat-form-field class="">
<input matInput type="password" placeholder="Password"
formControlName="password" required>
<mat-error
*ngIf="password.hasError('required')">
This is required
</mat-error>
<mat-error
*ngIf="password.hasError('pattern')">
Password should contain at least one number
</mat-error>
</mat-form-field>
</div>
我想让电子邮件错误消息出现在类似于密码错误消息的输入字段下方。
【问题讨论】:
标签: angular-material angular7 angular-reactive-forms