【发布时间】:2015-01-16 18:31:57
【问题描述】:
我是 CoffeeScript 新手并尝试编写 Angular 控制器。但是,我认为我在变量范围界定方面存在问题。我的代码如下:
app.controller 'IndexController', ['$scope', 'Authentication', 'Items', 'Snackbar', ($scope, Authentication, Items, Snackbar) ->
activate = ->
itemsSuccessFn = (data, status, headers, config) ->
vm.items = data.data
return
itemsErrorFn = (data, status, headers, config) ->
Snackbar.error data.error
return
Items().all().then itemsSuccessFn, itemsErrorFn
return
vm = this
vm.items = []
vm.isAuthenticated = Authentication.isAuthenticated()
activate()
return
]
问题出在 items 变量上。在activate 中运行itemsSuccessFn 时,该变量将填充来自服务的数据。但在主范围内,变量再次为空。我究竟做错了什么?我该如何解决?
如果还有关于代码的任何其他内容,应该做得更好,请告诉我,因为正如我所提到的,我刚刚开始学习。
【问题讨论】:
-
in the main scope the variable is again empty是什么意思?您是否使用“控制器作为”语法?示例:ng-controller="IndexController as vm"?