我遇到了一个特殊的问题,当我在'Logout'之前重新加载页面然后重新登录时,页面内容没有被加载。
这是我的代码
#login.js.coffee
app.controller "LoginController", ["$scope", "$rootScope",
($scope, $rootScope) ->
......
loginSuccess = (user) ->
$rootScope.$broadcast 'event:user-authenticated'
$scope.on "event:user-authenticated", ->
console.log "Event received in login.js.coffee"
--------------------------------------------------------------
#contnet.js.coffee
app.controller "ContentController", ["$scope", "$rootScope",($scope, $rootScope) ->
$scope.$on "event:user-authenticated", ->
console.log "Event received in content.js.coffee"
]
当我重新加载页面时观察到的问题 - >退出用户 - >重新登录
虽然我通过'$ rootScope'发送广播,但只有同一个控制器中的事件列表器正在工作。
仅在控制台中打印“login.js.coffee中收到的事件”日志,不记录其他消息。
有人可以帮忙吗?
答案 0 :(得分:1)
app.controller "LoginController", ["$scope", "$rootScope",
($scope, $rootScope) ->
......
loginSuccess = (user) ->
$rootScope.$broadcast 'event:user-authenticated'
$rootScope.on "event:user-authenticated", ->
console.log "Event received in login.js.coffee"
--------------------------------------------------------------
#contnet.js.coffee
app.controller "ContentController", ["$scope", "$rootScope",($scope, $rootScope) ->
$rootScope.$on "event:user-authenticated", ->
console.log "Event received in content.js.coffee"
]
使用$rootScope.$on
代替$scope.$on
来抓住$rootScope.broadcast