![](/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.