【发布时间】:2019-06-22 14:34:51
【问题描述】:
以https://material.angular.io/components/form-field/overview 为例
<div class="example-container">
<mat-form-field>
<input matInput placeholder="Enter your email" [formControl]="email" required>
<mat-error *ngIf="email.invalid">{{getErrorMessage()}}</mat-error>
</mat-form-field>
</div>
import {Component} from '@angular/core';
import {FormControl, Validators} from '@angular/forms';
/** @title Form field with error messages */
@Component({
selector: 'form-field-error-example',
templateUrl: 'form-field-error-example.html',
styleUrls: ['form-field-error-example.css']
})
export class FormFieldErrorExample {
email = new FormControl('', [Validators.required, Validators.email]);
getErrorMessage() {
return this.email.hasError('required') ? 'You must enter a value' :
this.email.hasError('email') ? 'Not a valid email' :
'';
}
}
错误似乎是在模糊时触发的。只有在离开输入后才会出现错误。在我的应用程序中也是如此。在当前模式下,存在错误,但直到输入失去焦点才会显示。
如何在输入更改时触发显示错误。
【问题讨论】:
标签: angular-material2 angular5