20

我正在使用角度材料 6,当在 mat-form-field 之后移动到正确显示的 mat-error 时,mat-form-field mat-error 没有显示。

不工作代码:

 <mat-form-field>
<input matInput type="time" formControlName="ToTime"/> <mat-error *ngIf="DaterForm.get('ToTime').hasError('InValidToTime')">FromTime Should be less than ToTime if choose a same date</mat-error>
     </mat-form-field>

工作正常:

 <input matInput type="time" formControlName="ToTime"/> </mat-form-field>
 <mat-error *ngIf="DaterForm.get('ToTime').hasError('InValidToTime')">FromTime Should be less than ToTime if choose a same date</mat-error>

有人解释了为什么在该控件中不起作用。

现场演示: stackblitz

4

3 回答 3

26

mat-error的,默认情况下不显示。它仅在输入为 时显示touched

errorStateMatcher但是,幸运的是,您可以使用绑定到mat-input元素的输入属性覆盖此行为。

添加此功能的拉取请求。

用法 :

<mat-form-field>
    <input matInput [errorStateMatcher]="matcher" [matDatepicker]="picker" placeholder="Choose a Start date" 
    formControlName="FromDate"
      [min]="minFromDate" 
           [max]="maxToDate" >
    <mat-datepicker-toggle matSuffix [for]="picker" ></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
    <mat-error >Please provide a valid Fromdate</mat-error> 
  </mat-form-field> 

所以你必须以ErrorStateMatcher这种方式在你的代码中实现。

export class MyErrorStateMatcher implements ErrorStateMatcher {
  isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
    const isSubmitted = form && form.submitted;
    return (control && control.invalid);
  }
}

matcher并在您的组件中为类添加一个新对象ErrorStateMatcher,该对象将作为一个值[errorStateMatcher]="matcher"

matcher = new MyErrorStateMatcher();

我还在您的分叉堆栈闪电战中添加了相同的代码

建议

您无需提供ngIf条件来mat-error指定您的formControlName. 它将根据mat-form-field它所在的位置自动考虑。

于 2018-07-21T14:53:21.407 回答
3

我找到了一个非常简单的解决方案,无需覆盖 ErrorStateMatcher 类,只需在 app.module.ts 中导入即可

1-导入库:

import { ErrorStateMatcher, ShowOnDirtyErrorStateMatcher } from '@angular/material/core'; 

2-添加到提供者,如:

@NgModule({
  providers: [
    AuthService,
    UserService,
    { provide: ErrorStateMatcher, useClass: ShowOnDirtyErrorStateMatcher }
  ],
})

重新提供应用程序。

Reza Taba于 2021 年 1 月 2 日更新

但是,上述简短解决方案存在一个问题:如果用户在触摸(模糊)后将该字段留空,则不会显示所需的错误。

请参阅我的解决方案以进行修复。

于 2018-07-25T12:34:39.347 回答
1

在全局范围内,在键入或触摸时显示 mat-error:与提供的解决方案不同,此方法将处理所有 mat-errors,而无需对每个输入字段应用匹配器。

1- 创建touched-error-state.matcher.ts文件:

import {FormControl, FormGroupDirective, NgForm } from '@angular/forms';
import {ErrorStateMatcher} from '@angular/material/core';

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

2- 在app.module导入中:

import { ErrorStateMatcher } from '@angular/material/core';
import { TouchedErrorStateMatcher } from './your-folder-path/touched-error-state.matcher';

3-现在将其提供给提供者:

@NgModule({
  providers: [
    AuthService,
    UserService,
    { provide: ErrorStateMatcher, useClass: TouchedErrorStateMatcher }
  ],
})

4-重新服务应用程序。

于 2021-01-02T17:30:59.973 回答