In my application I want to provide a checkbox for users to toggle whether the app periodically polls for new data in the background.
I have setup a watch to do the data polling. And i know that if i assign a variable to that watch when created I can call that variable and the watch will be disabled/unregistered.
My issue is that when the users clicks the textbox to on i want the watch to come back to life. I have not found the best way to get that done.
Here is my sample fiddle which somewhat illustrates the issue. I think if i could get it to work there i can rig up the rest of it.
The part that is not working is the part where the watch needs to come back on line:
if ($scope.counter === 15) {
$scope.$watch('counter', theWatchFunction);
$scope.test = "back on";
}
You cannot simply unregister an watcher and wait for it to automatically re-register again. The moment you run
myWatch()everything inside thetheWatchFunctionwill never run again. You can register a new watcher but not inside thetheWatchFunction.You can, for example, re-register the
counterwatcher on thetimewatcher: http://jsfiddle.net/bmleite/Lw3Lp/