Javascript AngularJS:清除 $watch
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14957614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS : Clear $watch
提问by kamaci
I have a watch function in my AngularJS application.
我的 AngularJS 应用程序中有一个监视功能。
$scope.$watch('quartzCrystal', function () {
...
}
However, after some condition (in my example, changing the page at my single-page application) I want to stop that watch (as like clearing timeout).
但是,在某些情况下(在我的示例中,在我的单页应用程序中更改页面)我想停止该监视(就像清除超时一样)。
How can I do that?
我怎样才能做到这一点?
回答by Umur Kontac?
$watchreturns a deregistration function. Calling it would deregister the $watcher.
$watch返回一个注销函数。调用它会注销$watcher.
var listener = $scope.$watch("quartz", function () {});
// ...
listener(); // Would clear the watch
回答by Anders Ekdahl
scope.$watch returns a function that you can call and that will unregister the watch.
scope.$watch 返回一个您可以调用的函数,该函数将取消注册手表。
Something like:
就像是:
var unbindWatch = $scope.$watch("myvariable", function() {
//...
});
setTimeout(function() {
unbindWatch();
}, 1000);
回答by SoEzPz
You can also clear the watch inside the callback if you want to clear it right after something happens. That way your $watch will stay active until used.
如果您想在发生某些事情后立即清除它,您也可以清除回调中的手表。这样你的 $watch 将保持活动状态直到被使用。
Like so...
像这样...
var clearWatch = $scope.$watch('quartzCrystal', function( crystal ){
if( isQuartz( crystal )){
// do something special and then stop watching!
clearWatch();
}else{
// maybe do something special but keep watching!
}
}
回答by Rewar
If you have too much watchers and you need to clear all of them, you can push them into an array and destroy every $watchin a loop.
如果你有太多的观察者并且你需要清除所有它们,你可以将它们推入一个数组并$watch在循环中销毁每个。
var watchers = [];
watchers.push( $scope.$watch('watch-xxx', function(newVal){
//do something
}));
for(var i = 0; i < watchers.length; ++i){
if(typeof watchers[i] === 'function'){
watchers[i]();
}
}
watchers = [];
回答by Manish Kumar
Ideally, every custom watch should be removed when you leave the scope.
理想情况下,当您离开示波器时,应该移除每个自定义手表。
It helps in better memory management and better app performance.
它有助于更好的内存管理和更好的应用程序性能。
// call to $watch will return a de-register function
var listener = $scope.$watch(someVariableToWatch, function(....));
$scope.$on('$destroy', function() {
listener(); // call the de-register function on scope destroy
});
回答by naCheex
Some time your $watch is calling dynamicallyand it will create its instances so you have to call deregistration function before your $watchfunction
有时你的 $watch 正在调用dynamically它会创建它的实例,所以你必须在你的$watch函数之前调用注销函数
if(myWatchFun)
myWatchFun(); // it will destroy your previous $watch if any exist
myWatchFun = $scope.$watch("abc", function () {});
回答by shailendra pathak
For discarding the copy of watchers, you can use this:
要丢弃观察者的副本,您可以使用:
watchers = void 0;

