<form (ngSubmit)="saveChanges()">
<mat-form-field appearance="outline" >
<mat-label>New ID</mat-label>
<input required matInput name="newName" [style]="elemStyles" [formControl]="email">
<mat-error *ngIf="email.invalid && (email.dirty || email.touched)">
<p class="error-message" *ngIf="email.errors.duplicateId">ID <strong>already</strong> exists.</p>
<p class="error-message" *ngIf="email.errors.required">This field is <strong>required.</strong></p>
</mat-error>
</mat-form-field>
<div class="action-buttons">
<button type="submit" [disabled]="email.invalid" mat-mini-fab class="accept action-button">
<mat-icon fontSet="fontawesome" fontIcon="fa-check" class="fa-xs action"></mat-icon>
</button>
<button type="submit" mat-mini-fab class="cancel action-button">
<mat-icon fontSet="fontawesome" fontIcon="fa-times" class="fa-xs action"></mat-icon>
</button>
</div>
</form>
mat-error 部分仅在离开输入字段的焦点后显示。接受按钮被禁用。任何信息如何解决此行为以在用户键入时获取错误消息?
我也试过删除(email.dirty || email.touched)
,但它不起作用:/
我的自定义验证器:
export class DuplicateIDValidator {
static duplicateIDValidator(pageStructure: PageStructureService) {
return (control: AbstractControl): {[key: string]: any} | null => {
return pageStructure.pages.every(page => page.questionId !== control.value.toLowerCase()
|| page.questionId === pageStructure.selectedPages[0].questionId) ? null : {duplicateId: true};
};
}
}