来自外部角度7的值的抽象控制

时间:2019-03-05 10:56:45

标签: javascript angular typescript

当我打开角度材料对话框时,我会向对话框发送一些数据

在对话框组件上也是如此 我创建自定义验证以检查该值是大于还是小于,但是我不断收到错误消息,指出属性未定义

这是我的代码

  constructor(@Inject(MAT_DIALOG_DATA) public data: VendorDataMaster,
  private fb: FormBuilder,
  public usd: UserdetailsService ) { }

this.validateFormVendor = this.fb.group({
      InvoiceAmountcontrol: [ this.data.InvoiceAmount , [ Validators.required, this.ToleranceValidator] ],
      NoInvoicecontrol: [ this.data.NoInvoice, [ Validators.required ] ],
      DateInvoicecontrol: [ this.data.DateInvoice, [ Validators.required ] ],
      NoFakturPajakcontrol: [ this.data.NoFakturPajak, [ Validators.required ] ],
      DateFakturPajakcontrol: [ this.data.DateFakturPajak, [ Validators.required ] ],
      Statuscontrol : [ {value:this.data.StatusInv, disabled: true} ]
    });
  }
  ToleranceValidator(control: AbstractControl) {
// this is when the data is undifiend
      console.log(this.data.AmountGR)
      if (parseInt(control.value) >  parseInt(this.data.AmountGR) * 1) {
        return { ToleranceRange : true };
      }
      return null;
  }
  public hasError = (controlName: string, errorName: string) =>{
    return this.validateFormVendor.controls[controlName].hasError(errorName);
  }

我猜ngif的启动速度比承包商快,但我不知道会发生什么

我只能尝试使用try catch来解决问题, 而且我将结果放在导出的组件上,这样我就可以在对话框组件上使用它,但是仍然无法防御

2 个答案:

答案 0 :(得分:0)

ToleranceValidator设置为async验证器,如下所示。而不是将其添加到数组的第二个值中,而是将其添加到第三位。

this.validateFormVendor = this.fb.group({
      InvoiceAmountcontrol: [ this.data.InvoiceAmount , [ Validators.required], this.ToleranceValidator ],
      NoInvoicecontrol: [ this.data.NoInvoice, [ Validators.required ] ],
      DateInvoicecontrol: [ this.data.DateInvoice, [ Validators.required ] ],
      NoFakturPajakcontrol: [ this.data.NoFakturPajak, [ Validators.required ] ],
      DateFakturPajakcontrol: [ this.data.DateFakturPajak, [ Validators.required ] ],
      Statuscontrol : [ {value:this.data.StatusInv, disabled: true} ]
    });
  }

答案 1 :(得分:0)

使用绑定

this.validateFormVendor = this.fb.group({
      InvoiceAmountcontrol: [ '' , [ Validators.required, this.ToleranceValidator.bind(this)] ],
      NoInvoicecontrol: ['', [ Validators.required ] ],
      DateInvoicecontrol: [ '', [ Validators.required ] ],
      NoFakturPajakcontrol: [ '', [ Validators.required ] ],
      DateFakturPajakcontrol: [ '', [ Validators.required ] ],
      Statuscontrol : [ {value:'', disabled: true} ]
    });
  }