如果我 运行 ng-repeat 对象或 returns 对象的函数有什么区别(性能)

Is there any difference (performance) if I run ng-repeat over object or function that returns object

我将服务中的一个数组绑定到 控制器 中的本地范围(通过引用传递):

myApp.controller('TheController', function($scope, theService){
    $scope.theArray = theService.array;
})

在役:

//...
theService.array = [];
    $http.get('/array').then(function(data){
        theService.array = angular.extends(theService.array, data);// here I'm updating theService.array with new values;
    })

该绑定的问题是我无法中断 对初始数组的引用。所以我必须做一些额外的功能来检测新旧数组之间的区别。

解决方法是每次都初始化数组,return 我的 service:

中的一个函数
$http.get('/array').then(function(data){
        theService.array = data;
    })

theService.getArray = function(){
    return theService.array;
    };

在控制器中它将是:

myApp.controller('TheController', function($scope, theService){
        $scope.theArray = theService.getArray;
    })

在我的 html(这里是问题)中,我将有:

<div ng-repeat="data in theArray()">....</div>

而不是<div ng-repeat="data in theArray ">....</div>

ng-repeat 的性能有问题吗?

据我所知,在 AngularJS' 摘要循环中,模板中的所有函数都将被再次调用(就像将函数放入 ng-if 时发生的情况一样)它们可能会导致另一个摘要周期 运行ned。

请参阅 this and this 了解更多说明,尽管他们并没有真正具体地谈论这件事。

所以我宁愿 运行 ng-repeat 而不是对象,并在确实应该更新值时偶尔更新它们。