Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Angular debug script for getting watchers of a certain directive or DOM element. Based on: https://gist.github.com/kentcdodds/31c90402750572107922
function getWatchers(root) {
root = angular.element(root || document.documentElement);
console.log('You selected this element:', root);
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 [];
}
}
function getWatcherExpressions (element) {
var watchers = getElemWatchers(element);
var watchlist = {
'_total': watchers.length
};
angular.forEach(watchers, function (watcher) {
watchlist[watcher.exp] = watchlist[watcher.exp] || [];
watchlist[watcher.exp].push(watcher);
});
return watchlist;
}
return getWatcherExpressions(root);
}
getWatchers();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment