Skip to content

Instantly share code, notes, and snippets.

@alexalannunes
Created October 23, 2019 13:47
Show Gist options
  • Save alexalannunes/043a7d8808467218dfd6b9a8b82dcbb2 to your computer and use it in GitHub Desktop.
Save alexalannunes/043a7d8808467218dfd6b9a8b82dcbb2 to your computer and use it in GitHub Desktop.
vm.count = function() {
var root = angular.element(document.getElementsByTagName("body"));
var watchers = [];
var f = function(element) {
angular.forEach(["$scope", "$isolateScope"], function(scopeProperty) {
if (element.data() && element.data().hasOwnProperty(scopeProperty)) {
angular.forEach(element.data()[scopeProperty].$$watchers, function(
watcher
) {
watchers.push(watcher);
});
}
});
angular.forEach(element.children(), function(childElement) {
f(angular.element(childElement));
});
};
f(root);
// Remove duplicate watchers
var watchersWithoutDuplicates = [];
angular.forEach(watchers, function(item) {
if (watchersWithoutDuplicates.indexOf(item) < 0) {
watchersWithoutDuplicates.push(item);
}
});
$scope.result = watchersWithoutDuplicates.length;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment