【问题标题】:set error " errors : {__zone_symbol__state: null, __zone_symbol__value: Array(0)}" by Angular 4 validatorAngular 4验证器设置错误“错误:{__zone_symbol__state:null,__zone_symbol__value:Array(0)}”
【发布时间】:2018-08-18 00:32:19
【问题描述】:

Mobile mobileNumberValidator 返回正常,但其他两个验证器在错误时返回 null 承诺。无法识别我在这里所做的错误。请帮忙。 这两个验证器在 formbuilder 对象中设置错误如下

错误 : {__zone_symbol__state: null, __zone_symbol__value: Array(0)}

在表单生成器组中

 this.empInfoForm = this.fb.group({
      'Mobile': [null, [Validators.required], [this.mobileNumberValidator.bind(this)]],
      'Private': [null,  [this.privateNumberValidator.bind(this)]],
      'Work': [null, [this.workNumberValidator.bind(this)]],  
    });

验证器方法

  mobileNumberValidator(control: AbstractControl) {

        return new Promise(resolve => {
          setTimeout(() => {
            if (!this.isMobileNumberValid) {
              resolve({
                'phoneNumber': true

              })
            console.log(this.empInfoForm)
            } else {
              resolve(null);
            }
          }, 10);
        })
      }
        workNumberValidator(control: AbstractControl) {

        return new Promise(resolve => {
          setTimeout(() => {
            if (!this.isWorkNumberValid) {
              resolve({
                'phoneNumber': true

              })
               console.log(this.empInfoForm)
            } else {
              resolve(null);
            }
          }, 10);
        })
      }
        privateNumberValidator(control: AbstractControl) {

        return new Promise(resolve => {
          setTimeout(() => {
            if (!this.isPrivateNumberValid) {
              resolve({
                'phoneNumber': true

              })

                  console.log(this.empInfoForm)
            } else {
              resolve(null);
            }
          }, 10);
        })
      }

【问题讨论】:

    标签: angular validation typescript asynchronous promise


    【解决方案1】:

    如果你使用 Promise,你的回调中应该有 2 个参数:

    return new Promise((resolve, reject) => { ...});
    

    当你想将错误设置为真时,你需要拒绝承诺:

    if (!this.isMobileNumberValid) {
      resolve({'phoneNumber': true});
      console.log(this.empInfoForm)
    } else { reject(null); }
    

    【讨论】:

    • 它给出了一个“ERROR Error: Uncaught (in promise): null”错误 workNumberValidator(control: AbstractControl) { return new Promise((resolve, reject) => { setTimeout(() => { if (!this.isWorkNumberValid) { resolve({ 'phoneNumber': true }) console.log(this.empInfoForm) } else { reject(null); } }, 10); }) }
    • 对不起,你应该返回false,忘记了:)
    • 没找到你。你能解释一下吗? :)
    • reject(false)替换reject(null),验证器处理布尔值
    • 仍然无法正常工作:(我仍然收到错误:{__zone_symbol__state: null, __zone_symbol__value: Array(0)}
    【解决方案2】:

    我发现了我在这里犯的错误。自定义验证器绑定应该是这里的第三个参数。

    this.empInfoForm = this.fb.group({
          'Mobile': [null, [Validators.required], [this.mobileNumberValidator.bind(this)]],
          'Private': [null,null,[this.privateNumberValidator.bind(this)]],
          'Work': [null,null,[this.workNumberValidator.bind(this)]],  
        });
    

    【讨论】:

      【解决方案3】:
      In IONIC
      
      if you facing issue in IONIC
      
      error :- __zone_symbol__state: null, __zone_symbol__value: Array(0)
      
      
      ======================== Sol
      
      
      
      
      // use platform ready function, its must
      
       this.platform.ready().then(() => {
      
              // use promise like syntax not like this.appVersion.getVersionNumber()
             
           this.appVersion.getVersionNumber().then((data) => {
                  
                  })
      
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-25
        • 2015-08-24
        • 2018-10-25
        • 2018-03-27
        • 2017-12-13
        • 1970-01-01
        • 2017-06-08
        • 1970-01-01
        相关资源
        最近更新 更多