更改数组时ng-repeat不更新 [英] ng-repeat is not updating when array is changed
问题描述
我有一个ng-repeat,它在更改正在使用的数组中的数据时不会更新.我已经研究了一段时间,但似乎没有任何效果.最初,当页面加载时,ng-repeat显示数据集的第一页,在获取新数据(下一页)并使用该数据设置该数组时,ng-repeat不会注意到更改,也不会填充更新的数组.如果有人可以在正确的方向上指导我,将不胜感激.
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>
推荐答案
尝试将tbody包裹在div中,但将控制器包裹在div中:
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>
为避免混淆,我建议将$ scope.getBusinessSites命名为$ scope.businessSites:)
and I suggest naming the $scope.getBusinessSites to $scope.businessSites for avoiding confusion :)
这篇关于更改数组时ng-repeat不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!