【问题标题】:ng-repeat is not updating when array is changed更改数组时 ng-repeat 不更新
【发布时间】:2016-03-11 22:57:58
【问题描述】:

我有一个 ng-repeat,它在更改它正在使用的数组中的数据时不会更新。我已经研究了很长时间,但似乎没有任何效果。最初,当页面加载时,ng-repeat 显示数据集的第一页,在获取新数据(下一页)并使用此数据设置该数组时,ng-repeat 不会注意到更改并且永远不会填充更新的数组。如果有人能在这方面引导我朝着正确的方向前进,我们将不胜感激。

gatekeeper.controller('businessController', ['$scope', 'siteService', function($scope, siteService) {

$scope.page = 1;
$scope.resultsPerPage = 50;
$scope.maxPaginationSite = 10;
$scope.pageCount = 0;
$scope.resultCount = 0;
$scope.getBusinessSites = [];

function getBusinessSites()
{
    siteService.getBusinessSites($scope.page, $scope.resultsPerPage).then(function(response) {
        $scope.getBusinessSites = response.data;
        console.log($scope.getBusinessSites);
        $scope.resultCount = response.data[0].QueryCount;
        $scope.page = response.data[0].Page;
        $scope.pageCount = Math.ceil($scope.resultCount / 50);
    });
}
getBusinessSites();
$scope.pageChanged = function () {
    $scope.page = this.page;
    getBusinessSites($scope.page, $scope.resultsPerPage);

};

}]);

<tbody ng-controller="businessController">

        <tr ng-repeat="site in getBusinessSites">
            <td>{{ site.SiteName }}</td>

            <td class="tableButton">

                <button ng-controller="ModalCtrl" type="button" class="btn btn-default" ng-click="open('lg')">
                    {{ site.ClientName }}
                </button>

                <br />
                <b>Facilities:</b>

                No Data Yet

            </td>

            <td>{{ site.Subdomain }}</td>

            <td>
                <a href={{ site.URL}}> {{ site.URL}} </a>

                <br />

                <b>Go-live Date: </b> No Data Yet

            </td>

            <td>No Data Yet</td>
            <td>{{site.ChannelPartner}}</td>
            <td>No Data Yet</td>
            <td>No Data Yet</td>
            <td>No Data Yet</td>
            <td>No Data Yet</td>
        </tr>
        <div >
            <uib-pagination class="pull-right" boundary-link-numbers="true" max-size="maxPaginationSite" boundary-links="true" total-items="resultCount" ng-model="page" ng-change="pageChanged()"></uib-pagination>
        </div>
    </tbody>

【问题讨论】:

    标签: angularjs angularjs-ng-repeat


    【解决方案1】:

    问题是 ng-repeat 已经引用了这个初始空数组 [],当你更改 $scope.getBusinessSites 时,你改变了这个变量的引用,但是 ng-repeat 仍然引用了内存中的那个空数组。

    因此,解决方案是直接将数据写入数组您的 ng-repeat 引用。您可以使用angular.extend 函数来做到这一点:

    改变这一行:

    $scope.getBusinessSites = response.data;
    

    关于这个:

    angular.extend($scope.getBusinessSites, response.data);
    


    更新: 此外,如果您不使用一次加载数据,则需要清除该数组中先前加载的数据:
    // empties an array
    $scope.getBusinessSites.length = 0;
    

    【讨论】:

    • 非常感谢,进行更改以清空数组和 angular.extend 成功了。 AngularJS 很棒,但在同样的方面,可能会非常令人沮丧。再次感谢!
    • 不客气。只是不要错过基础知识。 ;) 变量和数组引用,——这一切都不是 AngularJS 的特性,而是 JS。祝你好运!
    • 我认为它和你描述的一样,我创建了一个简单的 plnkr,你能帮忙解释一下为什么这里改变参考是有效的...plnkr.co/edit/7atCfQAnTbikUaGtgYi8?p=preview
    • @huanfeng 真的吗? :) 不记得为什么要以防万一。尝试重现问题未成功。即使在角度 1.3 中,这也很有效。如果您能重现该问题,我很乐意调查原因。
    • @doroshko,也许这个人已经回答了这个问题。 stackoverflow.com/questions/29849293/…
    【解决方案2】:

    尝试将 tbody 包装在 div 中,但将控制器包装在 div 中:

    <div ng-controller="BusinessController">
        <tbody>   
            <tr ng-repeat="site in getBusinessSites">
            .
            .
            .
        </tbody>
    </div>
    

    我建议将 $scope.getBusinessSites 命名为 $scope.businessSites 以避免混淆:)

    【讨论】:

    • 同意 - 拥有 getBusinessSites 和 $scope.getBusinessSites 只是不必要的混乱。
    • 感谢您的建议。命名事物可能很难!不过更有意义。
    【解决方案3】:

    ng-repeat 创建自己的范围。尝试添加 $parent 以在当前控制器范围内引用您的变量

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多