【问题标题】:Error: undefined is not an object (evaluating '$scope.inputcode.password')错误:未定义不是对象(评估“$scope.inputcode.password”)
【发布时间】:2016-03-27 08:44:31
【问题描述】:

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");
      }
    }
  }

这是我不断收到的错误:

错误:未定义不是对象(评估“$scope.inputcode.password”)

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

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

提前致谢:)

【问题讨论】:

    标签: javascript html angularjs ionic-framework


    【解决方案1】:

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

    $scope.inputcode = {};
    

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

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多