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