AngularJs Memory Leak : scope $$watchers increase even if the watched elements are removed -
i've got big issue angularjs application...
i load html content server (custom forms). of course, loaded html contains ng-show
, ng-click
etc...
so, $compile before append in page.
that's works fine.
but, each time $compile
loaded html, adds more $$watchers
in scope (certainly ng-show watchers).
here little demo/simulation : http://plnkr.co/edit/6ssazsfaugze5xmcyks7
my problem : that $$watchers
won't never decrease , cause memory leak if click hundreds time on "load some".
i've tried remove()
, empty()
, unbind()
elements (the links) $$watchers array keep growing , never cleaned.
how can resolve issue ? how can clean useless $$watchers or "uncompile" ?
thanks !!!
since new elements compiled same scope , on same element, watchers never removed.
all watchers removed when scope destroyed (scope.$destroy()
) happens automatically when element removed. element never removed, keep replacing html new nodes.
a clean way avoid avoid recompiling every new links, instead generate dynamic ng-repeat
list.
otherwise, if want keep code, can create new scope element every time compile it. way, when recompiling it, previous scope destroyed , watchers removed.
you can see in action here. have done replace scope
scope.$new()
in compile call new scope set element every time:
http://plnkr.co/edit/pvuayyb0iuoat3dvmpgm?p=preview
ps: interesting use case never faced though, can contribute angular community :-)
Comments
Post a Comment