(Es posible que deba cambiar bodya htmlo donde sea que coloque su ng-app)
(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);
}
});
console.log(watchersWithoutDuplicates.length);
})();
Gracias a erilem por señalar que esta respuesta no se realizó la $isolateScopebúsqueda y los observadores posiblemente se duplicaron en su respuesta / comentario.
Gracias a Ben2307 por señalar que es 'body'posible que sea necesario cambiarlo.
Original
Hice lo mismo, excepto que verifiqué el atributo de datos del elemento HTML en lugar de su clase. Corrí el tuyo aquí:
http://fluid.ie/
Y obtuve 83. Corrí el mío y obtuve 121.
(function () {
var root = $(document.getElementsByTagName('body'));
var watchers = [];
var f = function (element) {
if (element.data().hasOwnProperty('$scope')) {
angular.forEach(element.data().$scope.$$watchers, function (watcher) {
watchers.push(watcher);
});
}
angular.forEach(element.children(), function (childElement) {
f($(childElement));
});
};
f(root);
console.log(watchers.length);
})();
También puse esto en el mío:
for (var i = 0; i < watchers.length; i++) {
for (var j = 0; j < watchers.length; j++) {
if (i !== j && watchers[i] === watchers[j]) {
console.log('here');
}
}
}
Y no se imprimió nada, así que supongo que el mío es mejor (en el sentido de que encontró más relojes), pero me falta un conocimiento angular íntimo para saber con certeza que el mío no es un subconjunto adecuado del conjunto de soluciones.
$scopetiene una matriz $$ watchers con el número de observadores en ese controlador (bueno, si tienes alguna repetición ng o algo que crea otro alcance, eso no funciona tan bien). Pero creo que no hay forma de ver todos los relojes en toda la aplicación.