简体   繁体   English

Angularjs指令删除手表?

[英]Angularjs directive remove watch?

I have a $scope.$watch declared in the controller of a directive. 我有一个$scope.$watch在指令的控制器中声明。 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? 如果是这样,我如何检测指令何时被删除?

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. 当一个范围死亡时,它会产生一个$ destroy事件。 You can watch it: 你可以看:

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

and you can manually detach $watchers from the scope, by calling the function it returns: 并且您可以通过调用它返回的函数手动从范围中分离$ watchers:

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

You can do this with $on too. 你也可以用$ on来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM