输入值未定义在函数

时间:2016-05-11 13:39:08

标签: javascript angularjs twitter-bootstrap

这是我部分观点的一部分。

<div class="form-group row">
    <label for="password" class="col-sm-2 form-control-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="password" placeholder="Password" ng-model="password" required>
    </div>
  </div>

  <div class="form-group row">
    <label for="confirmPassword" class="col-sm-2 form-control-label">Confirm Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="confirmPassword" placeholder="Confirm Password" required>
    </div>
  </div>

这是我的控制器的一部分 -

self.password = document.getElementById("password");
        self.confirmPassword = document.getElementById("confirmPassword");

       angular.element(self.confirmPassword).on("input", function () {
            var closest = self.confirmPassword.closest('div');
            console.log(self.password.value); //undefined
            if(self.password.value != self.confirmPassword.value) {
                    closest.classList.remove("has-success");
                    closest.className += " has-error";
            } else {
                closest.classList.remove("has-error");
                closest.className += " has-success";   
            };
        });

为什么函数内部self.password.value未定义。我的最终目标是在密码不等于确认密码时添加类has-error,如果密码等于确认密码,则添加类has-success

1 个答案:

答案 0 :(得分:1)

您的代码中存在很多错误。