如何计算AngularJS应用程序中的绑定数量

时间:2016-02-29 21:34:11

标签: angularjs data-binding

如何计算AngularJS应用程序中数据绑定的数量?

1 个答案:

答案 0 :(得分:0)

function getScopes(root) {
    var scopes = [];
    function traverse(scope) {
        scopes.push(scope);
        if (scope.$$nextSibling)
            traverse(scope.$$nextSibling);
        if (scope.$$childHead)
            traverse(scope.$$childHead);
    }
    traverse(root);
    return scopes;
}
var rootScope = angular.element(document.querySelectorAll("[ng-app]")).scope();
var scopes = getScopes(rootScope);
var watcherLists = scopes.map(function(s) { return s.$$watchers; });
_.uniq(_.flatten(watcherLists)).length;

参考:http://larseidnes.com/2014/11/05/angularjs-the-bad-parts/