javascript 在 AngularJS 中只注册一次事件监听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24490429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Register event listener only once in AngularJS
提问by Kitze
I'm broadcasting an event from my navbar controller to another controller, but if I initialize the controller multiple times (when I'm going front and back through the application) the function that executes on my $on
event runs multiple times because it's registered multiple times.
我正在将一个事件从我的导航栏控制器广播到另一个控制器,但是如果我多次初始化控制器(当我前后通过应用程序时)在我的$on
事件上执行的函数会多次运行,因为它被多次注册.
$rootScope.$on('submitBookingDialog', function(){
submitBookingDialog();
});
How can I prevent the submitBookingDialog()
to happen more than once?
我怎样才能防止这种submitBookingDialog()
情况发生不止一次?
I found a solution, but I don't know if it's ideal.
我找到了一个解决方案,但我不知道它是否理想。
回答by DRiFTy
First of all, do you need to send the event on the $rootScope
? If not, then you could just register your event handler on the $scope
. The event handler will be destroyed whenever your controller scope is destroyed. You would then send the event via $scope.$emit
or $scope.$broadcast
depending on your controller hierarchy.
首先,您是否需要在$rootScope
? 如果没有,那么您只需在$scope
. 每当您的控制器范围被销毁时,事件处理程序就会被销毁。然后,您将通过$scope.$emit
或$scope.$broadcast
取决于您的控制器层次结构发送事件。
That being said, all you need to do to destroy your event listener is call the deregistration function that is returned when registering the listener:
话虽如此,销毁事件侦听器所需要做的就是调用注册侦听器时返回的注销函数:
var offSubmitBookingDialog = $rootScope.$on('submitBookingDialog', function(){
submitBookingDialog();
});
$scope.$on('$destroy', function() {
// Call the deregistration function when the scope is destroyed
offSubmitBookingDialog();
});
回答by Kim Biesbjerg
This seems to do it for me:
这似乎对我有用:
var removeListener = $rootScope.$on('submitBookingDialog', function(){
submitBookingDialog();
// Remove listener
removeListener();
});
回答by Hashbrown
For posterity I ended up doing this:
为了后代,我最终这样做了:
.run(function($rootScope) {
$rootScope.once = function(e, func) {
var unhook = this.$on(e, function() {
unhook();
func.apply(this, arguments);
});
};
})
Because I kept needing to do this in a few places this just ended up being cleaner.
With that on your app module you can now just call once
instead of $on
:
因为我一直需要在几个地方这样做,所以最终变得更干净了。
在您的应用程序模块上使用它,您现在可以调用once
而不是$on
:
$rootScope.once('submitBookingDialog', function() {
submitBookingDialog();
});
回答by callmehiphop
I ran into a similar situation, so I wrote a small library to make pub/sub stuff easier.
我遇到了类似的情况,所以我写了一个小库,让发布/订阅的东西更容易。
回答by user854301
Maybe you shoud unsubscribe on controller destroy event
也许你应该取消订阅控制器销毁事件
var removeSubmitBookingDialog = $rootScope.$on('submitBookingDialog',submitBookingDialog);
$scope.$on("$destroy", removeSubmitBookingDialog);