ng-repeat is not updating when array is changed

谁说我不能喝 提交于 2019-11-29 11:56:23

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 :)

The problem is that ng-repeat has referenced on this initial empty array [], when you change $scope.getBusinessSites, you change this variable's reference, but ng-repeat still reference on that empty array in memory.

So, solution is write data directly to array your ng-repeat reference. You can do it with angular.extend function:

Change this line:

$scope.getBusinessSites = response.data;

On this one:

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


UPD: Also, if you use loading data not once, you'll need to clear previously loaded data in that array:
// empties an array
$scope.getBusinessSites.length = 0;

The ng-repeat create its own scope. Try adding $parent to reference your variable on your current controller scope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!