首页 文章

未捕获的TypeError:无法以离子反应形式读取null的属性'value'

提问于
浏览
-1

我在一个离子项目工作 . 当使用角度响应式自定义验证时 . 它抛出一个未捕获的TypeError:无法读取null的属性“value”

this.signup = this.formBuilder.group({
          name: ['', Validators.compose([Validators.required, ValidateName])],
          email: ['', Validators.compose([Validators.required, ValidateEmail])],
          mobile: [
            '',
            Validators.compose([
              Validators.required,
              Validators.minLength(10),
              Validators.maxLength(15),
              ValidateMobile
            ])
          ],
          password: [
            '2',
            Validators.compose([Validators.required, Validators.minLength(6)])
          ],
          cpassword: [
            '1',
            Validators.compose([
              Validators.required,
              Validators.minLength(6),
              passwordValidat.bind(this)
            ])
          ]
        });
      }

   passwordValidat(control: AbstractControl) {
    if (control.get('password').value === control.get('cpassword').value) {
      return { invalid: true };
    }
    return { validUrl: true };
  }

当我试图运行此代码得到这样的错误

core.js:1449 ERROR错误:未捕获(在保证中):TypeError:无法读取null的属性'value'TypeError:无法在SignupPage.webpackJsonp.169.SignupPage.passwordValidat(signup.ts:)中读取null的属性'value' . 71)at forms.js:759

1 回答

  • 0
    passwordValidat(control: AbstractControl) {
        const  pwd = control.get('password');
        const cpwd = control.get('cpassword');
    
        console.log('pwd: ', pwd);
        console.log('cpwd: ', cpwd);
    
        if(pwd && cpwd) {
    
              if( pwd.value === cpwd.value ) {
                  return { invalid: true };
              }
                  return { validUrl: true };
              }
    
        } else {
            return { invalid: true };
        }
    
    }
    

相关问题