且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angularjs 指令删除手表?

更新时间:2023-02-26 21:39:43

这取决于作用域,而不是指令.如果范围被破坏,那么它的所有 $watchers 都会随之消亡.在页面更改时,您的范围会被 angular 破坏,所以您应该是安全的.

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.

当一个范围死亡时,它会产生一个 $destroy 事件.你可以看看:

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

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

你可以手动从作用域中分离 $watchers,通过调用它返回的函数:

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

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

你也可以用 $on 来做到这一点.

You can do this with $on too.