簡體   English   中英

捕獲TypeError:無法讀取未定義的屬性“ push”

[英]Getting Uncaught TypeError: Cannot read property 'push' of undefined

頁面加載時出現錯誤。 我正在嘗試將新對象追加到條目數組。 這怎么了

在此處輸入圖片說明

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>

您忘記在外部定義$ scope.newEntry = {},並且用ng-sumbit =“ submit()”形式的括號將代碼修改為。

$ scope.entries = [{名稱:“ Larry”},{名稱:“ Curly”},{名稱:“ Moe”}]];

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM