我试图理解为什么这段代码执行一个警告()3次:
$scope.logout = function () {
$rootScope.auth.$logout();
$rootScope.$on('$firebaseSimpleLogin:logout', function(event) {
alert("Logged out"); // This appears 3 times.
});
} 但这只是一个:
$scope.logout = function () {
$rootScope.auth.$logout();
alert("Logged out"); // This JUST appears once.
}我知道第二种方法是直接的,首先执行一行,然后再执行另一行。但是,如果$logout失败了,应用程序显示操作在没有成功的情况下成功了怎么办?由于这种可能性,我正在使用$firebaseSimpleLogin:logout事件来正确地处理这种情况。可悲的是,它并没有像我想象的那样起作用。
有什么不对的?
发布于 2014-05-12 02:11:43
很难说,如果没有看到应用程序的其余部分,但是有一个错误:在第一个代码示例中,每次调用$scope.logout时都会附加另一个事件侦听器--例如,如果您调用它两次,那么下次事件触发时它会发出两次警报。再次单击它,下次事件发生时,您将收到三个警报。您应该将事件侦听器的注册移到函数调用之外:
// put this anywhere that's only called once
app.run(function($rootScope) {
$rootScope.$on("$firebaseSimpleLogin:logout", ...);
});
// elsewhere
$scope.logout = function() {
$rootScope.auth.$logout();
};
// You could also unregister the function when you're done with it instead
app.controller("AuthController", function($scope, $rootScope) {
var unregListener = $rootScope.$on("$firebaseSimpleLogin:logout", ...);
$scope.$on("$destroy", unregListener);
$scope.logout = function() { ... };
});https://stackoverflow.com/questions/23582612
复制相似问题