简体   繁体   English

Angular2 / 4材料设计, <mat-error> 不工作 - 没有找到结果

[英]Angular2/4 material-design, <mat-error> not working - when no results found

I am using Angular4 Material Design with WebAPI - I am trying to get to display a validation message - "No matches found" when a user enter an invalid value in the (Material Auto Complete). 我正在使用带有WebAPI的Angular4 Material Design - 当用户在(材料自动完成)中输入无效值时,我试图显示验证消息 - “找不到匹配项”。

I have tried few things based on google searches:- https://material.angular.io/components/input/overview#custom-error-matcher https://github.com/angular/material2/blob/4383f51a58de1fccad2ed64443a5e22ab435c02b/src/demo-app/input/input-demo.ts#L56 我根据谷歌搜索尝试了一些东西: - https://material.angular.io/components/input/overview#custom-error-matcher https://github.com/angular/material2/blob/4383f51a58de1fccad2ed64443a5e22ab435c02b/src/演示应用程序/输入/输入demo.ts#L56

and below is my current implementation:- 以下是我目前的实施: -

HTML HTML

     <div class="form-group spaces" style="width: 30%">
     <mat-form-field>
     <input  [errorStateMatcher]="myErrorStateMatcher.bind(this)" matInput 
     placeholder="NUMBER" [matAutocomplete]="auto" type="text" 
     formControlName="validNumber" required [formControl]="stateCtrl">
     <md-error *ngIf="validNumbers?.length === 0"> No Matching Records 
     Found!
     </md-error>
    </mat-form-field>

    <mat-autocomplete #auto="matAutocomplete" mat-no-cache="true">
    <mat-option *ngFor="let c of validNumbers " [value]="c">{{c}}</mat-
    option>
    </mat-autocomplete>
    </div>

Typescript:- 打字稿: -

 import { ErrorStateMatcher } from '@angular/material/core';
 public validNumbers;

 myErrorStateMatcher(control, form): boolean
 {
    if (this.validNumbers && !this.validNumbers.length)
    {
        return true;
    }

    // Error when invalid control is touched, or submitted
    const isSubmitted = form && form.submitted;
    return !!(control.invalid && (control.touched || isSubmitted));
  }

Error message attached:- 附上错误信息: - 错误信息

myErrorStateMatcher needs to be a class instance, not a function. myErrorStateMatcher需要是一个类实例,而不是一个函数。 Also, .bind(this) isn't necessary / doesn't do anything since you this is not accessible in the template. 此外, .bind(this)是没有必要的/没有做任何事情,因为你this不是在模板中访问。

See docs: https://material.angular.io/components/input/overview#custom-error-matcher 请参阅文档: https//material.angular.io/components/input/overview#custom-error-matcher

You should be able to just create an object with isErrorState : 您应该能够使用isErrorState创建一个对象:

@Component(...)
class MyComponent {
  ...
  myErrorStateMatcher = {
    isErrorState(control, form) {
      /* do your check here */
    }
  }

If that doesn't work you can also try myErrorStateMatcher: ErrorStateMatcher = . 如果这不起作用,您还可以尝试myErrorStateMatcher: ErrorStateMatcher = Ultimately you may need to / want to create a class: 最终,您可能需要/想要创建一个类:

class MyErrorStateMatcher implements ErrorStateMatcher

that has the isErrorStateMethod you're using and then set myErrorStateMatcher = new MyErrorStateMatcher in your component. 具有您正在使用的isErrorStateMethod ,然后在组件中设置myErrorStateMatcher = new MyErrorStateMatcher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM