I'm working with AngularJS and I'm trying to monitor a variable for changes in order to decrease another variable. This is the code snippet I have so far:
$scope.$watch('watchMe', function () {
$scope.decreaseMe--;
)
The watchMe variable is accessible across all scopes. I am using only one controller which handles both the function that modifies watchMe and the watch itself.
Each time watchMe changes, the watch function runs twice due to how Angular functions. However, this causes decreaseMe to be decremented twice as well, leading to an incorrect value.
I've attempted to compare old and new values but haven't been successful in resolving the issue.
When watchMe changes once, these are the values I observe for old/new/decreased:
old: 7 new: 8
decreasing 5
old: 7 new: 8
decreasing 4
How can I adjust my code to ensure decreaseMe is decreased only once when watchMe changes?
Update: