我有一个应用程序,在我的所有控制器都注册后手动启动。然后我想看到当用户登录时,他们的帐户已经过验证。不幸的是我的范围监听器无法正常工作,无法捕获从我的登录控制器发出的广播。我的帐户验证代码如下:
class CMSAccountCtrl
constructor: (@$scope, @$rootScope, @$location, @$http, @APGlobalState) ->
console.log @$scope
@$scope.$on 'CMSAccountCtrl-verify', (event, value) =>
console.log 'cms account ctrl should have verified'
@verify()
@$scope.verify = () =>
@verify()
verify: () =>
@$http({
'method': 'POST'
'url': "#{window.ap_base_uri}/caas/account/verify"
'data': {
'apCookie': @APGlobalState.get 'cookie'
}
}).success((data, status, headers, config) =>
# Do something
console.log(data)
if data.isVerified == 'yes'
@$rootScope.$broadcast 'CMSLoginCtrl-showToolbar'
else
@$scope.alert = {
msg: "You're account as not been verified. Please re-check your email."
}
).error((data, status, headers, config) =>
# Do somethings
)
if !window.apInject?
window.apInject = {}
if !window.CMSControllers?
window.CMSControllers = {}
# This executes before bootstrap, but after angular.module 'myModule', []
window.apInject.CMSAccountCtrl = (app) ->
app.controller 'CMSAccountCtrl', [
'$scope'
'$rootScope'
'$location'
'$http'
'APGlobalState'
CMSAccountCtrl
]
window.CMSControllers.CMSAccountCtrl = CMSAccountCtrl
奇怪的是,我的@ $ scope。$ on'listener'适用于我的一些控制器,但即使它们都实现了相同的结构,也会失败。为什么我的听众没有注册?
答案 0 :(得分:0)
您可以$发出事件或$广播事件。两者之间的区别在于$ emit将向上调度范围层次结构。但是,$ broadcast会将事件向下发送到其子范围。
在我们的$ scope。$ broadcast不工作的情况下,尝试执行$ scope。$ emit,因为接收器将处于向上水平。