更改数组时,ng-repeat 不会更新 [英] ng-repeat is not updating when array is changed

查看:25
本文介绍了更改数组时,ng-repeat 不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 = [];函数 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 = 函数 () {$scope.page = this.page;getBusinessSites($scope.page, $scope.resultsPerPage);};

}]);

<tr ng-repeat="getBusinessSites 中的站点"><td>{{site.SiteName }}</td><td class="tableButton"><button ng-controller="ModalCtrl" type="button" class="btn btn-default" ng-click="open('lg')">{{ site.ClientName }}<br/><b>设施:</b>还没有数据</td><td>{{site.Subdomain }}</td><td><a href={{site.URL}}>{{site.URL}} </a><br/><b>上线日期:</b>还没有数据</td><td>没有数据</td><td>{{site.ChannelPartner}}</td><td>没有数据</td><td>没有数据</td><td>没有数据</td><td>没有数据</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>

</tbody>

解决方案

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

<tr ng-repeat="getBusinessSites 中的站点">...</tbody>

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

I have an ng-repeat that isn't updating upon changing the data in the array that it is using. I've researched for quite a while but nothing seems to be working. Initially, when the page loads, the ng-repeat displays the first page of a dataset, upon getting new data (the next page) and setting that array with this data, the ng-repeat isn't noticing the change and never populates with the updated array. It would be greatly appreciated if someone could steer me in the right direction on this.

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>

解决方案

Try wrapping the tbody inside of a div and but the controller in the div:

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

and I suggest naming the $scope.getBusinessSites to $scope.businessSites for avoiding confusion :)

这篇关于更改数组时,ng-repeat 不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆