[英]Angularjs: why there are 3 watchers for 1 binding?
Please take a look at the screenshot given below 请看下面的截图
As you can see in the screenshot above there are #3 watchers for a single binding. 如您在上面的屏幕快照中所见,单个绑定具有#3观察者。
Can anyone please elaborate why is it so? 谁能解释为什么会这样吗?
PS: I am using AngularJS Batarang for checking the performance. PS:我正在使用AngularJS Batarang检查性能。
var app = angular.module('app', []); app.controller('appCtrl', function ($scope, $timeout) { $scope.name = 'vikas bansal'; })
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script> <script src="app.js"></script> </head> <body> <div ng-app="app" ng-controller="appCtrl"> {{name}} </div> </body> </html>
I think Angular Batarang has a wrong counter of watchers. 我认为Angular Batarang的观察者计数器有误。 I checked with few different sources, and all except AngularJS Batarang show me single watcher on your code. 我检查了几个不同的资源,除AngularJS Batarang之外的所有资源都向我展示了您代码上的单个监视程序。 Check out this question with function: 使用功能查看此问题 :
(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);
})();
And you can check Watchers extension for chrome. 您也可以检查Watchers扩展程序中的chrome。 Both show 1 watcher. 两者都显示1个观察者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.