错误:undefined不是对象(正在评估'$scope.inputcode.password')

Error: undefined is not an object (evaluating '$scope.inputcode.password')

本文关键字:scope password inputcode 评估 undefined 对象 错误      更新时间:2023-09-26

HTML:

<form ng-submit="mylogin()">
      <div class="list">
        <label class="item item-input">
          <span class="input-label">Username</span>
          <input type="text" ng-model="inputcode.username">
        </label>
        <label class="item item-input">
          <span class="input-label">Password</span>
          <input type="password" ng-model="inputcode.password">
        </label>
        <label class="item">
          <button class="button button-block" type="submit">Log in</button>
        </label>
      </div>
    </form>
<br>
{{inputcode.password}}<---testing for correct syntax and it works fine here

控制器:

$scope.inputcode;
$scope.discountcodes=['phillyd','kevin','john'];
$scope.mylogin = function() {
    for (var i=0; i<$scope.discountcodes.length; i++) {
      if($scope.inputcode.password==$scope.discountcodes[i]){
        console.log($scope.discountcodes[i]);
      }
      else{
        console.log("You failed to crack the code young jedi");
      }
    }
  }

这是我不断得到的错误:

错误:undefined不是对象(评估"$scope.inputcode.password")

我已经测试了$scope.inputcode.password是否有效,并通过在我的html页面上显示{{inputcode.paassword}}来保存信息。

我不明白到底是什么错误?

提前感谢:)

您已经声明了$scope.inputcode,但不是作为对象声明的——因为它当前存在,它只是undefined。您需要将其初始化为一个对象,如下所示:

$scope.inputcode = {};

这将允许您将其作为对象进行评估。