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

时间:2016-03-27 00:41:44

标签: javascript html angularjs ionic-framework

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不是对象(评估&#39; $ scope.inputcode.password&#39;)

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

我不明白究竟是什么错误?

提前致谢:)

1 个答案:

答案 0 :(得分:1)

您已声明$scope.inputcode但未声明为对象 - 因为它当前存在,只是undefined。您需要将其初始化为对象,如下所示:

$scope.inputcode = {};

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