kam*_*aci 274 javascript watch angularjs
我的AngularJS应用程序中有一个watch功能.
$scope.$watch('quartzCrystal', function () {
...
}
Run Code Online (Sandbox Code Playgroud)
但是,在某些条件之后(在我的例子中,在我的单页应用程序中更改页面)我想要停止该监视(就像清除超时一样).
我怎样才能做到这一点?
Umu*_*acı 515
$watch返回注销功能.调用它会取消注册$watcher.
var listener = $scope.$watch("quartz", function () {});
// ...
listener(); // Would clear the watch
Run Code Online (Sandbox Code Playgroud)
And*_*ahl 47
范围.$ watch返回一个可以调用的函数,该函数将取消注册该表.
就像是:
var unbindWatch = $scope.$watch("myvariable", function() {
//...
});
setTimeout(function() {
unbindWatch();
}, 1000);
Run Code Online (Sandbox Code Playgroud)
SoE*_*zPz 21
如果您想在发生某些事情后立即清除它,您也可以清除回调中的手表.这样你的$ watch将一直保持活跃状态直到使用.
像这样......
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!
}
}
Run Code Online (Sandbox Code Playgroud)