你是否需要在$ scope $ destroy事件中取消绑定$ scope。$ on? [英] Do you need to unbind $scope.$on in $scope $destroy event?

查看:111
本文介绍了你是否需要在$ scope $ destroy事件中取消绑定$ scope。$ on?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用$ on绑定事件的指令我是否需要在范围被破坏时删除该绑定,还是自动完成?我还需要拨打$ element.off吗?

I have directive that bind event using $on do I need to remove that binding when scope is destroyed, or is it done automatically? Also do I need to call $element.off?

return {
    restrict: 'A',
    link: function($scope, $element, $attrs) {
        $element.on('load', function() {
            $element[0].contentWindow.focus();
        });
        $scope.$on('iframe:focus', function() {
            $element[0].contentWindow.focus();
        });
    }
};


推荐答案

$ scope。$ on () 将在您的视图中由于E2E绑定而丢失其表示时自动销毁/清除。请注意, $ rootScope。$ on()绑定不会发生这种情况。您还可以查看 AngularJS的$ scope文档

$scope.$on() listenerers will be destroyed / cleaned up automatically when it loses its representation due to E2E binding in your view. Note that this will not happen for $rootScope.$on() bindings. You could also take a look at the $scope documentation of AngularJS.


  • $ scope。$ on() ; 将自动销毁。

  • 您需要手动销毁 $ rootScope。$ on()

  • $scope.$on(); will be destroyed automatically.
  • You need to destroy $rootScope.$on() manually.

范围销毁 - 当不再需要子作用域时,子作用域创建者的
责任是通过
作用域销毁它们。$ destroy()API。这样做是为了阻止
$摘要调用传播到子作用域,并允许垃圾收集器回收
子作用域模型使用的内存。

Scope Destruction - When child scopes are no longer needed , it is the responsibility of the child scope creator to destroy them via scope.$destroy() API. This is done in order to stop propagation of $digest calls into the child scope and allow for memory used by the child scope models to be reclaimed by the garbage collector.



如何销毁 $ rootScope的示例。$ on()



Example of how to destroy $rootScope.$on():

//bind event
var registerScope = $rootScope.$on('someEvent', function(event) {
    console.log("fired");
});

// clean up
$scope.$on('$destroy', registerScope);






plnkr 将向您显示 $ scope的不同行为。$ on() $ rootScope。$ on()



通过切换此 plunkr 控制器将被重新绑定到您的视图。每次切换视图时都会绑定 $ rootScope。$ on(); 事件,而不会破坏之前视图的事件绑定。这样, $ rootScope。$ on()侦听器将被叠加/相乘。这不会发生在 $ scope。$ on()绑定上,因为它将通过切换视图而被销毁(在DOM中丢失E2E绑定表示)。


This plnkr will show you the different behaviors of $scope.$on() and $rootScope.$on().

By switching the view in this plunkr the controller will be rebinded to your view. The $rootScope.$on(); event is binded every time you switch a view without destroying the event bindings of the view before. In that way the $rootScope.$on() listeners will be stacked/multiplied. This will not happen to the $scope.$on() bindings because it will be destroyed by switching the view (losing the E2E binding representation in DOM).


  • $ scope。$ on('event'); 将收听 $ scope。$ broadcast('event') &安培; $ rootScope。$ broadcast('event')

  • $scope.$on('event'); will listen to $scope.$broadcast('event') & $rootScope.$broadcast('event')

$ rootScope。 $ on('event'); 只会收听 $ rootScope。$ broadcast('event')

这篇关于你是否需要在$ scope $ destroy事件中取消绑定$ scope。$ on?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆