Skip to content

Instantly share code, notes, and snippets.

@msfrisbie
Created January 24, 2015 03:09
Show Gist options
  • Save msfrisbie/f39018bd6e44af1fd631 to your computer and use it in GitHub Desktop.
Save msfrisbie/f39018bd6e44af1fd631 to your computer and use it in GitHub Desktop.
Scope watcher counter
var getWatchers = function (element) {
// convert to a jqLite/jQuery element
// angular.element is idempotent
var el = angular.element(
// defaults to the body element
element || document.getElementsByTagName('body')
)
// extract the DOM element data
, elData = el.data()
// initalize returned watchers array
, watchers = [];
// AngularJS lists watches in 3 categories
// each contains an independent watch list
angular.forEach([
// general inherited scope
elData.$scope,
// isolate scope attached to templated directive
elData.$isolateScope,
// isolate scope attached to templateless directive
elData.$isolateScopeNoTemplate
],
function (scope) {
// each element may not have a scope class attached
if (scope) {
// attach the watch list
watchers = watchers.concat(scope.$$watchers || []);
}
}
);
// recurse through DOM tree
angular.forEach(el.children(), function (childEl) {
watchers = watchers.concat(getWatchers(childEl));
});
return watchers;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment