7
I have the following $scope.$watch
in the controller of a directive I created:
var interval;
$scope.$watch('active', function () {
if ($scope.active == true) {
interval = $interval(function () {
if ($scope.current >= $scope.to) {
// Quero destruir o Watch aqui.
return $interval.cancel(interval);
}
$scope.current += amounth;
}, 10);
}
});
When the value falls within the condition stated above by a comment, I would like the $watch
turned off/destroyed as I will no longer need to detect changes in the value active
.
Is there any way to destroy/disable a watch in Angular?
In your watch statement you assign it to a variable. When you want to destroy it you execute the function that will be in the variable:
var meuWatch = $scope.$watch('active', function ()...; meuWatch();
– Sorack
This works just like Jquery settimeout?
– Marconi
setTimeout
it’s not jQuery, it’s Javascript. And no, it’s not the same.– Wallace Maxters