当范围被破坏时,是否应该删除 angular $watch? [英] Should angular $watch be removed when scope destroyed?

查看:15
本文介绍了当范围被破坏时,是否应该删除 angular $watch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在从事一个项目,我们发现在不清除销毁范围内的广播订阅时会出现大量内存泄漏.以下代码已修复此问题:

Currently working on a project where we found huge memory leaks when not clearing broadcast subscriptions off destroyed scopes. The following code has fixed this:

var onFooEventBroadcast = $rootScope.$on('fooEvent', doSomething);

scope.$on('$destroy', function() {
    //remove the broadcast subscription when scope is destroyed
    onFooEventBroadcast();
});

这种做法是否也应该用于手表?下面的代码示例:

var onFooChanged = scope.$watch('foo', doSomething);

scope.$on('$destroy', function() {
    //stop watching when scope is destroyed
    onFooChanged();
});

推荐答案

不,您不需要删除 $$watchers,因为一旦作用域被销毁,它们将被有效地删除.

No, you don't need to remove $$watchers, since they will effectively get removed once the scope is destroyed.

来自Angular的源代码(v1.2.21),Scope$destroy方法:

From Angular's source code (v1.2.21), Scope's $destroy method:

$destroy: function() {
    ...
    if (parent.$$childHead == this) parent.$$childHead = this.$$nextSibling;
    if (parent.$$childTail == this) parent.$$childTail = this.$$prevSibling;
    if (this.$$prevSibling) this.$$prevSibling.$$nextSibling = this.$$nextSibling;
    if (this.$$nextSibling) this.$$nextSibling.$$prevSibling = this.$$prevSibling;
    ...
    this.$$watchers = this.$$asyncQueue = this.$$postDigestQueue = [];
    ...

因此,$$watchers 数组被清空(并且作用域从作用域层次结构中移除).

So, the $$watchers array is emptied (and the scope is removed from the scope hierarchy).

从数组中移除 watcher 就是 unregister 函数所做的一切:

Removing the watcher from the array is all the unregister function does anyway:

$watch: function(watchExp, listener, objectEquality) {
    ...
    return function deregisterWatch() {
        arrayRemove(array, watcher);
        lastDirtyWatch = null;
    };
}

因此,手动"取消注册 $$watchers 是没有意义的.

So, there is no point in unregistering the $$watchers "manually".

您仍然应该取消注册事件侦听器(正如您在帖子中正确提到的那样)!

You should still unregister event listeners though (as you correctly mention in your post) !

注意:您只需要取消注册在其他作用域上注册的侦听器.无需取消注册在正在销毁的作用域上注册的侦听器.
例如:

NOTE: You only need to unregister listeners registered on other scopes. There is no need to unregister listeners registered on the scope that is being destroyed.
E.g.:

// You MUST unregister these
$rootScope.$on(...);
$scope.$parent.$on(...);

// You DON'T HAVE to unregister this
$scope.$on(...)

(感谢@John for 指出)

此外,请确保您从比正在销毁的范围内存活的元素取消注册任何事件侦听器.例如.如果您有指令在父节点或 上注册侦听器,则您也必须取消注册它们.
同样,您不必删除在被销毁元素上注册的侦听器.

Also, make sure you unregister any event listeners from elements that outlive the scope being destroyed. E.g. if you have a directive register a listener on the parent node or on <body>, then you must unregister them too.
Again, you don't have to remove a listener registered on the element being destroyed.

与原始问题无关,但现在还有一个 $destroyed 事件在被销毁的元素上调度,因此您也可以使用它(如果它适合您的用例):

Kind of unrelated to the original question, but now there is also a $destroyed event dispatched on the element being destroyed, so you can hook into that as well (if it's appropriate for your usecase):

link: function postLink(scope, elem) {
  doStuff();
  elem.on('$destroy', cleanUp);
}

这篇关于当范围被破坏时,是否应该删除 angular $watch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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