【问题标题】:Uncaught TypeError: Cannot read property 'value' of null in ionic reactive form未捕获的类型错误:无法以离子反应形式读取 null 的属性“值”
【发布时间】:2018-11-19 09:47:30
【问题描述】:

我正在从事一个离子项目。使用角度反应形式自定义验证时。它抛出未捕获 TypeError:无法读取 null 的属性“值”

     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:不能 读取空类型错误的属性“值”:无法读取属性“值” 为空 在 SignupPage.webpackJsonp.169.SignupPage.passwordValidat (signup.ts:71) 在 forms.js:759

【问题讨论】:

  • 这意味着您的control.get() 调用之一正在返回null,请确保您在正确的时间正确地调用了该函数。
  • @DudeCoder 聊天的 cmets 应该被避免,他们会分散问题的注意力并弄乱评论部分...... SO提供聊天室chat.stackoverflow.com
  • Patrick Evans ,同时在不同的时间调试问题和 senario 得到相同的错误 control.get() 总是返回空值。你能告诉我一个正确的方法来做这个密码验证。并且可能我知道“我做错了什么?”
  • 我添加了一个答案,以提示您如何找出问题所在。您现在将看到哪个密码为空,如果其中一个为空,则不会再发生崩溃,但程序会返回无效对象。 — 使用浏览器的控制台监控正在发生的事情。

标签: javascript angular ionic2 ionic3


【解决方案1】:
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 };
    }

}

【讨论】:

    猜你喜欢
    • 2021-12-01
    • 2015-04-30
    • 2022-06-16
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多