Vik*_*sal 8 javascript angularjs angularjs-watch angularjs-bindings
请看下面给出的截图
正如您在上面的屏幕截图中看到的,单个绑定有#3观察者.
任何人都可以详细说明为什么会如此?
PS:我正在使用AngularJS Batarang来检查性能.
var app = angular.module('app', []);
app.controller('appCtrl', function ($scope, $timeout) {
$scope.name = 'vikas bansal';
})Run Code Online (Sandbox Code Playgroud)
<!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>Run Code Online (Sandbox Code Playgroud)
我认为 Angular Batarang 的观察者计数器是错误的。我检查了几个不同的来源,除了 AngularJS Batarang 之外,所有来源都向我展示了您代码上的单个观察者。用函数检查这个问题:
(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);
})();
Run Code Online (Sandbox Code Playgroud)
您可以检查Chrome 的Watchers扩展。两者均显示 1 个观察者。
| 归档时间: |
|
| 查看次数: |
425 次 |
| 最近记录: |