最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs directive remove watch? - Stack Overflow

programmeradmin3浏览0评论

I have a $scope.$watch declared in the controller of a directive. When I change pages and the directive is removed do I have to manually destroy the way? If so how do I detect when the directive has been removed?

I have a $scope.$watch declared in the controller of a directive. When I change pages and the directive is removed do I have to manually destroy the way? If so how do I detect when the directive has been removed?

Share Improve this question asked Jun 6, 2013 at 7:20 HarryHarry 54.9k76 gold badges185 silver badges270 bronze badges 1
  • Angular should take care of that for you – gion_13 Commented Jun 6, 2013 at 7:27
Add a comment  | 

1 Answer 1

Reset to default 21

It depends on the scope, not the directive. If the scope is destroyed, then all its $watchers die with it. On page change your scope'll be destroyed by angular, so you should be safe.

When a scope dies it yields a $destroy event. You can watch it:

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

and you can manually detach $watchers from the scope, by calling the function it returns:

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

You can do this with $on too.

发布评论

评论列表(0)

  1. 暂无评论