当Angular Material字段中未显示mat-error时,可能是由于以下几个原因引起的:
确保你正确使用了mat-form-field指令。它应该包裹你的表单字段,并且mat-error应该放在mat-form-field中。
Username is required
确保你的表单字段绑定了一个FormControl,并且你在模板中正确引用了它。
import { FormControl, Validators } from '@angular/forms';
usernameFormControl = new FormControl('', Validators.required);
确保你在mat-error中正确设置了验证条件。例如,如果你使用了Validators.required,则需要在mat-error中使用*ngIf="usernameFormControl.hasError('required')"
。
Username is required
确保你正确引入了Angular Material相关模块,并将它们添加到你的模块的imports数组中。
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
@NgModule({
imports: [
MatInputModule,
MatFormFieldModule,
// other imports
],
// other declarations
})
export class YourModule { }
通过检查并修复以上几个可能的问题,你应该能够解决Angular Material字段中未显示mat-error的问题。