-
-
Save deltreey/930890db340675366ce4 to your computer and use it in GitHub Desktop.
Toggle an AngularJS $watch expression. The default $watch can only be toggled once.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var toggleWatch = function(watchExpr, fn) { | |
| var watchFn; | |
| return function() { | |
| if (watchFn) { | |
| watchFn(); | |
| watchFn = undefined; | |
| console.log("Disabled " + watchExpr); | |
| } else { | |
| watchFn = $scope.$watch(watchExpr, fn); | |
| console.log("Enabled " + watchExpr); | |
| } | |
| }; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var testWatcher = toggleWatch("watchTest", function(value) { | |
| $log.info("Got " + value); | |
| }); | |
| testWatcher(); // Enables | |
| testWatcher(); // Disables | |
| testWatcher(); // Enables again |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment