Last active
January 23, 2018 11:01
-
-
Save smonusbonus/7abefc85be97925b938bca6205e177de to your computer and use it in GitHub Desktop.
Angular debug script for getting watchers of a certain directive or DOM element. Based on: https://gist.github.com/kentcdodds/31c90402750572107922
This file contains 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
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