通过Angular 4验证器设置错误“errors:{__ zone_symbol__state:null,__ zone_symbol__value:Array(0)}”

时间:2018-03-09 12:20:06

标签: angular validation typescript asynchronous promise

移动mobileNumberValidator返回正常但其他两个验证器在错误时返回空承诺。不能确定我在这里做的错误。请帮忙。 这两个验证器在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);
        })
      }

3 个答案:

答案 0 :(得分:0)

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

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

如果您想将错误设置为true,则需要拒绝承诺:

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

答案 1 :(得分:0)

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

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)]],  
    });

答案 2 :(得分:0)

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) => {
            
            })

})