SORU
28 AĞUSTOS 2013, ÇARŞAMBA


Nasıl bir sayfada saatlerin toplam sayısını saymak için?

Tüm sayfaya açısal saatler sayısını saymak için bir yol, JavaScript, var mı?

Batarang, ama her zaman ihtiyaçlarımıza uygun değil mi kullanıyoruz. Bizim uygulama büyük ve otomatik testler eğer seyretmek sayısı en fazla giderse kontrol etmek için kullanarak ilgileniyoruz.

Ayrıca, her kumanda için ayrı ayrı saatler saymak için yararlı olacaktır.

Editburada benim girişimi. Sınıf ng-scope ile her şeyi izliyor sayılır.

(function () {
    var elts = document.getElementsByClassName('ng-scope');
    var watches = [];
    var visited_ids = {};
    for (var i=0; i < elts.length; i  ) {
       var scope = angular.element(elts[i]).scope();
       if (scope.$id in visited_ids) 
         continue;
       visited_ids[scope.$id] = true;
       watches.push.apply(watches, scope.$$watchers);
    }
    return watches.length;
})();

CEVAP
30 AĞUSTOS 2013, Cuma


html body değiştirmek gerekebilir veya ng-app nereye koyarsan koy ()

(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);
})();
  • Bu cevap belirttiğin için erilem için teşekkürler $isolateScope arama ve gözlemcileri potansiyel olarak onun cevap/yorum/çoğaltılmış eksikti.

  • 'body' değiştirilmesi gerekebilir buna dikkat çektiğin için Ben2307 için teşekkürler.


Özgün

Verileri HTML öğesi yerine kendi sınıfının nitelik kontrol ettim hariç aynı şeyi yaptım. Senin buraya koştum:

http://fluid.ie/

Ve 83. Benim koştum ve 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);
})();

Ben de benimkini koy:

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');
        }
    }
}

Ve hiçbir şey yazdırdınız o yüzden sanırım benimki daha iyi (o buldu daha saatler) - ama yoksun samimi açısal bilgi için emin o benim değil düzgün bir alt çözüm kümesi.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Adam Outler

    Adam Outler

    19 EKİM 2006
  • Cristina Landa

    Cristina Lan

    28 Ocak 2010
  • Glyn Dewis

    Glyn Dewis

    25 AĞUSTOS 2007