Skip to content

Instantly share code, notes, and snippets.

@techyaura
Last active May 9, 2019 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techyaura/0e9a79a4e0a954b8f598be8c4fda8f51 to your computer and use it in GitHub Desktop.
Save techyaura/0e9a79a4e0a954b8f598be8c4fda8f51 to your computer and use it in GitHub Desktop.
function to calculate no of watchers in angular js (v1.3.2 & onwards)
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);
}
// watchers length
getWatchers().length;
// get all watchers on the whole page
getWatchers();
// get watchers of a specific element (and its children)
getWatchers(document.body);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment