我在 Angular js 上关注 railscasts #405,他使用的代码如下
app = angular.module("Raffler", ["ngResource"])
app.factory "Entry", ["$resource", ($resource) ->
$resource("/entries/:id", {id: "@id"}, {update: {method: "PUT"}})
]
@RaffleCtrl = ["$scope", "Entry", ($scope, Entry) ->
$scope.entries = Entry.query()
$scope.addEntry = ->
entry = Entry.save($scope.newEntry)
$scope.entries.push(entry)
$scope.newEntry = {}
$scope.drawWinner = ->
pool = []
angular.forEach $scope.entries, (entry) ->
pool.push(entry) if !entry.winner
if pool.length > 0
entry = pool[Math.floor(Math.random()*pool.length)]
entry.winner = true
entry.$update()
$scope.lastWinner = entry
]
我试图用几乎相同的代码在我的应用程序中实现非常相似的东西,唯一的问题是,当调用 drawWinner 函数时,我的浏览器会记录错误“找不到变量条目”但变量条目是在函数 addEntry 中声明的?