Angular材料没有mat-error消息

时间:2018-03-27 14:11:11

标签: angular angular-material

我在材料表单验证时出错,从未在网页中显示过mat错误消息。这是代码:

<mat-form-field>
  <input matInput placeholder="{{'DEPARTMENT' | translate}}"
         [FormControl]="inputDepartmentControl"
         [(ngModel)]="departmentValue"
         name="departmentValue"
         [errorStateMatcher]="matcher"
         [matAutocomplete]="autoDepartment"
         (keyup)="onDepartmentChange()"
         (keydown)="keyDownFunction($event)">
  <mat-error *ngIf="inputDepartmentControl.invalid">error</mat-error>
  <mat-autocomplete #autoDepartment="matAutocomplete">
    <mat-option *ngFor="let department of filteredDepartments | async" [value]="department.id">
      <span>{{department.id}} | </span>
      <span>{{department.value.name}}</span>
    </mat-option>
  </mat-autocomplete>
  <!--<mat-error *ngIf="(departmentInputControl)?.invalid">{{'DEPARTMENT_VALUE_ERROR' | translate}}</mat-error>-->
</mat-form-field>

TS文件,我已经为测试添加了这个匹配器类:

export class MyErrorStateMatcher implements ErrorStateMatcher{

  isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
    return !!(control && control.invalid && (control.dirty || control.touched));
  }
}

组件类:

matcher = new MyErrorStateMatcher();
departmentValue = '';
inputDepartmentControl = new FormControl('', [Validators.maxLength(3)]);

除了验证之外,其他任何事情都有效。

任何人都可以告诉我如何在字符串的长度&gt;中显示错误消息3?

谢谢。

0 个答案:

没有答案