【问题标题】:Why is mat-error working on MatDialog but not on normal page?为什么 mat-error 在 MatDialog 上工作,但在普通页面上却不行?
【发布时间】:2019-08-27 04:25:59
【问题描述】:

我有一个普通的 HTML 页面,它有一个输入字段和一个按钮。

用户在输入字段中插入一些值,应用发出 HTTP 请求。

如果 HTTP 响应正常,则用户导航到其他页面,否则会显示 mat-error 警告用户他的搜索无效。

在我的垫子对话框中,我有完全相同的场景。

我遇到的问题是,在我的正常页面中,我必须按两次按钮才能出现我的 mat-error,但是,在我的 mat-dialog 中,mat-error 会立即出现。

代码完全一样。

这是我的代码:

MyComponent.component.ts & MyDialog.component.ts

public inputForm: FormControl;

ngOnInit() {
  this.inputForm = new FormControl('');
}

private getData(value: any, returnType: string) {
  if (value === undefined || value === "") {
    this.inputForm.setErrors({ 'invalid': true });
  } else {
    this.getOrder(value, returnType);
  }
}

private getOrder(value: any, returnType: string) {
  if (value.length > 0 && !isNaN(value)) {
    this.getOrderByID(value, returnType);
  } else if (value.length > 0 && isNaN(value)) {
    this.getOrderByNumber(value, returnType);
  }
}

private getOrderByID(value: number, returnType: string) {
  this.rest.getOrder(value).subscribe((orderIdData: {}) => {
    if (Object.entries(orderIdData).length !== 0) {
      this.rest.getReturnByOrderId(value).subscribe((returnOrdIdData: Return[]) => {
        if (Object.entries(returnOrdIdData).length !== 0) {
      this.returns = returnOrdIdData;
    } else {
      this.returns = [];
    }
    this.onCreateReturn(orderIdData, returnType);
      }, error => {
    if (error.status === 404) {
      this.returns = [];
    }
    this.onCreateReturn(orderIdData, returnType);
      });
    } else {
      this.inputForm.setErrors({ 'invalid': true });
    }
  }, error => {
    this.inputForm.setErrors({ 'invalid': true });
  });
}

private getOrderByNumber(value: string, returnType: string) {
  this.rest.getOrderByNumber(value).subscribe((orderNrData: {}) => {
    if (Object.entries(orderNrData).length !== 0) {
      this.rest.getReturnByOrderNumber(value).subscribe((returnOrdNrData: Return[]) => {
        if (Object.entries(returnOrdNrData).length !== 0) {
      this.returns = returnOrdNrData;
    } else {
      this.returns = [];
    }
    this.onCreateReturn(orderNrData, returnType);
      }, error => {
    if (error.status === 404) {
      this.returns = [];
    }
    this.onCreateReturn(orderNrData, returnType);
      });
    } else {
      this.inputForm.setErrors({ 'invalid': true });
    }
  }, error => {
    this.inputForm.setErrors({ 'invalid': true });
  });
}

private getErrorMessage() {
  return (<HTMLInputElement>document.getElementById("input-form-id")).value === "" ? 'Este campo é obrigatório!' : 'A encomenda que inseriu não existe!';
}

private onCreateReturn(el: {}, returnType: string) {
  this.setData(el, returnType);
  this.router.navigate(['returns-create']);
}

private isInputInvalid() {
  if (this.inputForm.invalid) return true;
    return false;
}

MyComponent.component.html & MyDialog.component.html

<div class="row">
  <div class="col-2 order-label">Order: </div>

  <div class="col-8 search-div">
    <mat-form-field class="search-form-field" appearance="standard">
      <input matInput class="order-input" id="input-form-id" placeholder="Ex: EU030327" [formControl]="inputForm" #searchInput>
      <mat-error *ngIf="isInputInvalid()">{{ getErrorMessage() }}</mat-error>
      <mat-hint>Insira o ID ou o Nº de uma encomenda.</mat-hint>
    </mat-form-field>
  </div>

  <div class="col-2"></div>
</div>

<br>

<button mat-raised-button color="accent" (click)="getData(searchInput.value, 'Refund')" [disabled]="isInputInvalid()">Refund</button>

当响应返回错误或响应为空时,我需要出现 mat-error,但这只是 MyDialog 组件中的情况...

为什么会这样?

谢谢!

【问题讨论】:

    标签: javascript node.js angularjs typescript angular-material


    【解决方案1】:

    对于任何可能在经过几天的反复试验后遇到此问题的人,我找到了一个可行的解决方案...

    我创建了一个布尔值isInputInvalid 并将其默认设置为 true。 每当我按下按钮时,都会执行 HTTP 请求,并且根据响应,isInputInvalid 布尔值设置为 true 或 false。 如果 HTTP 响应无效,我将其设置为 true,如果有效,我将其设置为 false。

    然后,如果布尔值为真,我将我的表单控件设置为无效。

    在我的 HTML 文件中,我用 &lt;form&gt; 包围了我的输入,并创建了一个 [formGroup]。 对于错误验证,我将*ngIf 内的&lt;mat-error&gt; 设置为等于searchForm.controls['inputForm'].invalid

    这是我的代码:

    MyComponent.component.ts

    private searchForm: FormGroup;
    private isInputInvalid: boolean = true;
    
    ngOnInit() {
      this.searchForm = new FormGroup({
        inputForm: new FormControl('')
      });
    }
    
    private getData(value: any, returnType: string) {
      if (value === undefined || value === "") {
        this.inputForm.setErrors({ 'invalid': true });
      } else {
        this.getOrder(value, returnType);
      }
    }
    
    private getOrder(value: any, returnType: string) {
      if (value.length > 0 && !isNaN(value)) {
        this.getOrderByID(value, returnType);
      } else if (value.length > 0 && isNaN(value)) {
        this.getOrderByNumber(value, returnType);
      }
    }
    
    private getOrderByID(value: number, returnType: string) {
      this.rest.getOrder(value).subscribe((orderIdData: {}) => {
        if (Object.entries(orderIdData).length !== 0) {
          this.rest.getReturnByOrderId(value).subscribe((returnOrdIdData: Return[]) => {
            if (Object.entries(returnOrdIdData).length !== 0) {
              this.isInputInvalid = false;
              this.returns = returnOrdIdData;
            } else {
              this.isInputInvalid = false;
              this.returns = [];
            }
            this.onCreateReturn(orderIdData, returnType);
          }, error => {
            this.isInputInvalid = true;
            if (error.status === 404) {
              this.returns = [];
            }
            this.onCreateReturn(orderIdData, returnType);
          });
        } else {
          this.isInputInvalid = true;
        }
      }, error => {
        this.isInputInvalid = true;
      });
    
      if(this.isInputInvalid){
        this.searchForm.controls['inputForm'].setErrors({ 'invalid': true });
      }
    }
    
    private getOrderByNumber(value: string, returnType: string) {
      this.rest.getOrderByNumber(value).subscribe((orderNrData: {}) => {
        if (Object.entries(orderNrData).length !== 0) {
          this.rest.getReturnByOrderNumber(value).subscribe((returnOrdNrData: Return[]) => {
            if (Object.entries(returnOrdNrData).length !== 0) {
              this.isInputInvalid = false;
              this.returns = returnOrdNrData;
            } else {
              this.isInputInvalid = false;
              this.returns = [];
            }
            this.onCreateReturn(orderNrData, returnType);
          }, error => {
            this.isInputInvalid = true;
            if (error.status === 404) {
              this.returns = [];
            }
            this.onCreateReturn(orderNrData, returnType);
          });
        } else {
          this.isInputInvalid = true;
        }
      }, error => {
        this.isInputInvalid = true;
      });
    
      if(this.isInputInvalid){
        this.searchForm.controls['inputForm'].setErrors({ 'invalid': true });
      }
    }
    
    private getErrorMessage() {
      return (<HTMLInputElement>document.getElementById("input-form-id")).value === "" ? 'Este campo é obrigatório!' : 'A encomenda que inseriu não existe!';
    }
    
    private onCreateReturn(el: {}, returnType: string) {
      this.setData(el, returnType);
      this.router.navigate(['returns-create']);
    }
    

    MyComponent.component.html

    <div class="row">
      <div class="col-2 order-label">Order: </div>
    
      <div class="col-8 search-div">
        <form [formGroup]="searchForm">
          <mat-form-field class="search-form-field" appearance="standard">
            <input matInput class="order-input" id="input-form-id" placeholder="Ex: EU030327" formControlName="inputForm" #searchInput>
            <mat-error *ngIf="searchForm.controls['inputForm'].invalid">{{ getErrorMessage() }}</mat-error>
            <mat-hint>Insira o ID ou o Nº de uma encomenda.</mat-hint>
          </mat-form-field>
        </form>
      </div>
    
      <div class="col-2"></div>
    </div>
    
    <br>
    
    <button mat-raised-button color="accent" (click)="getData(searchInput.value, 'Refund')" [disabled]="searchForm.controls['inputForm'].invalid">Refund</button>
    

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2022-06-19
      • 1970-01-01
      相关资源
      最近更新 更多