I understand that each model that I include in my view enters the digest loop and $watch
applied on it, since all Angular applications are basically one page apps. But when does it clear the $watch
?
For example, let's say that I have the page view1.html
with the template {{view.name1}}
. When I switch the route to the page view2.html
with the template {{view.name2}}
, does the $watch
on {{view.name1}}
is being unregistered automatically or should I do it manually?
When you invoke the $watch() method, to create a binding, Angular JS returns a "de-registration" function. This function can then be used to unbind your $watch() listener - all you have to do is invoke this returned function and your $watch() listener will be removed. Save this answer.
8,000+ users. Check how many watchers are active in an angular app. Angular watchers is the ultimate AngularJS tool which tells you how many active watchers you currently have. It automatically updates so you can see live how many watcher a page have. This is perfect to increase performance and debugging as well.
Mobile Support: With AngularJS even today you can build dynamic web pages it would lack support for mobile browsers. However, Angular offers support across mobile devices & browsers. Therefore, the migration ensures support for most kinds of mobile browsers and devices.
Each scope have a private array $$watchers
:
$watch
is called it pushes listeners into $$watchers
, see Scope#$watch.$digest
it iterates down all the $$watchers
, see Scope#$digest.$destroy
is called it cleans up all it's watchers, see Scope#$destroy
$destroy
to all of it's child scopes either isolated or not, so you'll never have an orphan scope.$destroy
for all directive scopes ( that are created within the $compile service), see here
$destroy
on these cases:You'll only need to call $destroy
when you manually create scopes ($new
/ $transclude
) and want them to be destroyed before their parent scope.
ngView
is probably a good example because you have only one element that is bound to it in your application and that element (with it's scope which often is the rootScope) is never destroyed. So angular automatically creates a new child scope($new
) when it enters a route and calls $destroy
on it when exists thus flushing all the watchers which were created by that route.
From Scope#$new docs:
$destroy() must be called on a scope when it is desired for the scope and its child scopes to be permanently detached from the parent and thus stop participating in model change detection and listener notification by invoking.
From $compile - transclusion functions docs:
if you intend to add and remove transcluded content manually in your directive (by calling the transclude function to get the DOM and and calling element.remove() to remove it), then you are also responsible for calling $destroy on the transclusion scope.
Take this as a rule, If you manually created a scope (with $new
or $transclude
) and it is not needed anymore but it's
parent scope does you need to manually destroy it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With