Skip to content

Instantly share code, notes, and snippets.

@OndeVai
Last active August 27, 2015 18:09
Show Gist options
  • Save OndeVai/2ff65620dae44a080734 to your computer and use it in GitHub Desktop.
Save OndeVai/2ff65620dae44a080734 to your computer and use it in GitHub Desktop.
Angular - Get number of watchers
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