问题描述
有没有一种方法,在JavaScript中,要计算整个页面上的角手表的数量?
我们使用<一个href=\"https://chrome.google.com/webstore/detail/angularjs-batarang/ighdmehidhipcmcojjgiloacoafjmpfk?hl=en\">Batarang,但它并不总是符合我们的需要。我们的应用程序是大,我们感兴趣的是使用自动化测试,以检查表计数上升太多。
这也将是有益的指望每个控制器根据手表。
修改:这里是我的尝试。它计算的一切带班纳克范围的手表。
(函数(){
VAR的ELT = document.getElementsByClassName('NG-范围');
变种手表= [];
变种visited_ids = {};
对于(VAR I = 0; I&LT; elts.length;我++){
VAR范围= angular.element(应急定位发射[I])的范围()。
如果(范围。在visited_ids的$ id)
继续;
visited_ids [$范围ID] =真;
watches.push.apply(手表,范围$$观察);
}
返回watches.length;
})();
(您可能需要更改体
到 HTML
或任何你把你的 NG-应用
)
(函数(){
VAR根= angular.element(document.getElementsByTagName('身体')); VAR观察家= []; 变种F =功能(元素){
angular.forEach(['$范围,$ isolateScope'],功能(scopeProperty){
如果(element.data()及&放大器; element.data()的hasOwnProperty(scopeProperty)){
angular.forEach(element.data()[scopeProperty]。$$观察家,功能(守望者){
watchers.push(守望者);
});
}
}); angular.forEach(element.children()函数(元素childElement){
F(angular.element(是childElement));
});
}; F(根); //删除重复观察
变种watchersWithoutDuplicates = [];
angular.forEach(观察家,函数(项目){
如果(watchersWithoutDuplicates.indexOf(项目)小于0){
watchersWithoutDuplicates.push(项目);
}
}); 的console.log(watchersWithoutDuplicates.length);
})();
-
感谢erilem指出了该答案缺少
$ isolateScope
搜索和观察家可能在他/她的回答/注释被复制。 -
感谢Ben2307的指出了
身体
可能需要改变。
原始
我做同样的事情,但我查了HTML元素的数据属性,而不是它的类。我跑你这里:
和得到83.我跑我的,得到了121。
(函数(){
VAR根= $(document.getElementsByTagName('身体'));
VAR观察家= []; 变种F =功能(元素){
如果(element.data()的hasOwnProperty('$范围')){
angular.forEach(element.data()$范围。$$观察家,功能(守望者){
watchers.push(守望者);
});
} angular.forEach(element.children()函数(元素childElement){
F($(是childElement));
});
}; F(根); 的console.log(watchers.length);
})();
我也把这个在我的:
为(VAR I = 0; I&LT; watchers.length;我++){
对于(VAR J = 0; J&LT; watchers.length; J ++){
如果(我== J&安培;!&安培;观察家[I] ===观察家[J]){
的console.log('这里');
}
}
}
和没有打印出来,所以我猜我的是更好的(因为它发现了更多的手表) - 但我缺乏亲密角知识肯定知道我的是不是解决方案集的一个子集
Is there a way, in JavaScript, to count the number of angular watches on the entire page?
We use Batarang, but it doesn't always suit our needs. Our application is big and we're interested in using automated tests to check if the watch count goes up too much.
It would also be useful to count watches on a per-controller basis.
Edit: here is my attempt. It counts watches in everything with class ng-scope.
(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;
})();
(You may need to change body
to html
or wherever you put your 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);
})();
Thanks to erilem for pointing out this answer was missing the
$isolateScope
searching and the watchers potentially being duplicated in his/her answer/comment.Thanks to Ben2307 for pointing out that the
'body'
may need to be changed.
Original
I did the same thing except I checked the data attribute of the HTML element rather than its class. I ran yours here:
And got 83. I ran mine and got 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);
})();
I also put this in mine:
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');
}
}
}
And nothing printed out, so I'm guessing that mine is better (in that it found more watches) - but I lack intimate angular knowledge to know for sure that mine isn't a proper subset of the solution set.
这篇关于怎么算一个页面上的手表总数是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!