Angularjs指令删除看? [英] Angularjs directive remove watch?
问题描述
我有一个 $范围。$手表指令的控制器声明
。当我更改页面和指令被删除我必须手动销毁的方式吗?如果是这样,我怎样检测时,该指令已被删除?
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?
推荐答案
这取决于范围,而不是指令。如果范围被破坏,那么它所有的$观察家死它。在页面更改scope'll由角被破坏,所以你应该是安全的。
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.
这篇关于Angularjs指令删除看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!