设置错误 " errors : {__zone_symbol__state: null, __zone_symbol__value: Array(0)}" 由 Angular 4 验证器



Mobile mobileNumberValidator 返回正常,但其他两个验证器在错误时返回空承诺。无法确定我在这里犯的错误。请帮忙。这两个验证器在表单构建器对象中设置错误,如下所示

错误:{__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);
        })
      }

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

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

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

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

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

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

最新更新