如果当范围破坏角$表中删除? [英] Should angular $watch be removed when scope destroyed?
问题描述
目前工作的一个项目,我们发现了巨大的内存泄漏时,不清除广播订阅了破坏范围。下面code已经修复了这个:
VAR onFooEventBroadcast = $ rootScope上('fooEvent',DoSomething的)$;(范围。在$('$破坏',功能){
//删除广播订阅范围时被销毁
onFooEventBroadcast();
});
如若这种做法也可以用于手表 code下面的例子:
VAR onFooChanged = $范围手表('富',DoSomething的)。(范围。在$('$破坏',功能){
//驻足观望范围时被销毁
onFooChanged();
});
没有,你并不需要删除 $$观察家
,因为它们会有效地去除得到一次的范围被破坏。
从角的源$ C $ C(v1.2.21),的范围
$摧毁
方法:
$破坏:功能(){
...
如果(家长$$ childHead ==本)父$$ childHead =这个$$ nextSibling。;
如果(家长$$ childTail ==本)父$$ childTail =这个$$prevSibling。;
如果(这$$prevSibling)这个$$prevSibling $$ nextSibling =这个$$ nextSibling。;
如果(这$$ nextSibling)。这个$$ nextSibling $$prevSibling =这个$$prevSibling。;
...
。这个$$观察家=这个$$ asyncQueue =这个$$ postDigestQueue = [];
...
因此, $$观察家
数组被清空(和范围是从范围层次结构中移除)。
删除观察家
从数组是所有DEREGISTER功能确实反正:
$表:功能(watchExp,监听器,objectEquality){
...
返回功能deregisterWatch(){
arrayRemove(数组,守望者);
lastDirtyWatch = NULL;
};
}
因此,有在注销的 $$观察家没有一点
手动。
您还是应该取消注册事件侦听器,但(为你正确地在您的文章提)!
注意:
你只需要注销的其他范围注册的侦听器。没有必要注销上被销毁范围注册的侦听器。结果
例如:
//你必须注销这些
$ rootScope $上(...)。
。$ $范围父在$(...)。//你不必注销该
$范围。在$(...)
<子>(感谢@约翰的<一个href=\"http://stackoverflow.com/questions/25113884/should-angular-watch-be-removed-when-scope-destroyed/25114028?noredirect=1#comment-49039216\">pointing吧)
此外,请确保您从活得比范围被破坏元素注销任何事件侦听器。例如。如果你有一个指令注册一个监听器父节点上或&LT;车身方式&gt;
,则必须注销他们太多结果
<子>同样,你也不必取消对被破坏的元素注册的侦听器。
无关类原来的问题,但现在也有在元件上的 $摧毁
事件调度被破坏,所以你可以挂接到,作为以及(如果这是适合您的用例):
链接:功能postLink(范围,ELEM){
doStuff();
elem.on('$毁灭',清理);
}
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();
});
Should this practice also be used for watches? Code example below:
var onFooChanged = scope.$watch('foo', doSomething);
scope.$on('$destroy', function() {
//stop watching when scope is destroyed
onFooChanged();
});
No, you don't need to remove $$watchers
, since they will effectively get removed once the scope is destroyed.
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 = [];
...
So, the $$watchers
array is emptied (and the scope is removed from the scope hierarchy).
Removing the watcher
from the array is all the deregister function does anyway:
$watch: function(watchExp, listener, objectEquality) {
...
return function deregisterWatch() {
arrayRemove(array, watcher);
lastDirtyWatch = null;
};
}
So, there is no point in deregistering the $$watchers
"manually".
You should still deregister event listeners though (as you correctly mention in your post) !
NOTE:
You only need to deregister listeners registered on other scopes. There is no need to deregister listeners registered on the scope that is being destroyed.
E.g.:
// You MUST deregister these
$rootScope.$on(...);
$scope.$parent.$on(...);
// You DON'T HAVE to deregister this
$scope.$on(...)
(Thx to @John for pointing it out)
Also, make sure you deregister 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 deregister them too.
Again, you don't have to remove a listener registered on the element being 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);
}
这篇关于如果当范围破坏角$表中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!