Angular directive removes clock?

I have $scope.$watch declared in the controller directive. When I change pages and delete the directive, do I have to manually destroy this method? If so, how do you determine when the directive was deleted?

+7
source share
1 answer

It depends on the scope and not on the directive. If the area is destroyed, then all of its $ watchers die with it. On the page, change the scope to angular, so you should be safe.

When the scope dies, it gives the $ destroy event. You can watch it:

 $scope.$on('$destroy', callback); 

and you can manually disable $ watchers from the scope by calling the return function:

 var sentinel = $scope.$watch('expression', callback); sentinel(); // kill sentinel 

You can do this with $ on.

+20
source

All Articles