Is it possible to set count
and not trigger the $watch
callback?
$scope.count=1;
$scope.$watch('count',function(){...});
Thanks.
Join Stack Overflow to learn, share knowledge, and build your career.
Is it possible to set count
and not trigger the $watch
callback?
$scope.count=1;
$scope.$watch('count',function(){...});
Thanks.
You could use setTimeout to wait for Angular's digest cycle to complete and then run your code:
setTimeout(function(){
$scope.count = 1;
},0)
This way the angular code finishes running, checks for changes and then the property is set without getting Angular involved.
$timeout
did not work probably because angular knew do another $digest
.
– ruffrey
Mar 12 '15 at 14:39
No, but you can add control variable logic within your callback
$scope.$watch('count',function(){
if ($scope.bypass) return;
//else ....
});
Kind of. You can use a $watch listener function. So instead of using:
$scope.$watch('count',function(){...});
You'd use:
scope.$watch(
function() {
// return the condition you do want evaluated. Whenever this return value changes
// from the previous $digest cycle the change handler function below will be called.
// The return value will be passed in as `newValue` (and the previous as `oldValue`)
// in the change handler.
},
function(newValue,oldValue){...});
Edit: Since it looks like you're trying to watch 2 different variables, another option may be to use watchCollection
which will trigger if either changes (so you could have one consolidated handler for both):
$scope.$watchCollection(['count','otherCount'], function(newValues, oldValues) { ...});
$digest
loop limit, then consider increasing it and sticking to using$watchers
to update the value. If you update the value from outside the Angular scope, then your application is going to end up in an inconsistent state. If the values do not converge, then you might be solving a symptom rather than a problem. – musically_ut Dec 8 '13 at 1:05$watch
on their sum (or concatenation) like this:$watch(function () { return scope.var1 + scope.var2; }, ...)
and then have only one watcher fire. – musically_ut Dec 8 '13 at 1:54