javascript - Cant find variable Angular JS -
i following railscasts #405 on angular js , code uses follows
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 ]
i tried implement similar in application practically same code issue when drawwinner function called browser logs error "cant find variable entry" variable entry declared in function addentry?
javascript functionally scoped. entry in addentry scoped function referenced $scope.addentry , not same variable created foreach loop... need re-check variable scope.
https://developer.mozilla.org/en-us/docs/web/javascript/reference/functions_and_function_scope
Comments
Post a Comment