Skip to content

Instantly share code, notes, and snippets.

@kasperlewau
Created January 29, 2016 14:35
Show Gist options
  • Save kasperlewau/d8bdb1cc0cc1f1d220cb to your computer and use it in GitHub Desktop.
Save kasperlewau/d8bdb1cc0cc1f1d220cb to your computer and use it in GitHub Desktop.
angular watch count
/**
* getWatchers().length - all the watchers on your page
* getWatchers($0).length - all the watchers within your selected element
*/
function getWatchers(root) {
root = angular.element(root || document.documentElement);
var watcherCount = 0;
function getElemWatchers(element) {
var isolateWatchers = getWatchersFromScope(element.data().$isolateScope);
var scopeWatchers = getWatchersFromScope(element.data().$scope);
var watchers = scopeWatchers.concat(isolateWatchers);
angular.forEach(element.children(), function (childElement) {
watchers = watchers.concat(getElemWatchers(angular.element(childElement)));
});
return watchers;
}
function getWatchersFromScope(scope) {
if (scope) {
return scope.$$watchers || [];
} else {
return [];
}
}
return getElemWatchers(root);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment