我在加载页面时收到错误。我试图将一个新对象附加到一个条目数组中。这有什么问题?
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>
答案 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 = {}
};