![](/img/trans.png)
[英]Getting “Uncaught TypeError: Cannot read property 'push' of undefined” sporadically
[英]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.