获取未捕获的TypeError:无法读取属性' push'未定义的

时间:2015-08-19 09:54:15

标签: javascript ruby-on-rails angularjs

我在加载页面时收到错误。我试图将一个新对象附加到一个条目数组中。这有什么问题?

enter image description here

raffle.js

angular.module('myApp', []).controller("RaffleCtrl", function ($scope) {
  $scope.entries = [
      {
        name: "Larry"
      }, {
        name: "Curly"
      }, {
        name: "Moe"
      }
    ]

  $scope.addEntry = function () {
    $scope.entries.push($scope.newEntry)
    $scope.newEntry = {}
  };

});

的index.html

<h1>Raffler</h1>

<div ng-controller="RaffleCtrl">

  <form ng-sumbit="addEntry">
    <input type="text" ng-model="newEntry.name">
    <input type="submit" value="Add">
  </form>

  <ul>
    <li ng-repeat="entry in entries">{{entry.name}}</li>
  </ul>
</div>

1 个答案:

答案 0 :(得分:2)

你忘了在外面定义$ scope.newEntry = {} 并且形式为ng-sumbit =&#34;提交()&#34; 将代码修改为..

$ scope.entries = [         {           姓名:&#34;拉里&#34;         },{           名称:&#34; Curly&#34;         },{           名称:&#34; Moe&#34;         }       ];

  $scope.newEntry = {}  
   $scope.submit = function() {
     $scope.entries.push($scope.newEntry)
     $scope.newEntry = {}
   };