ng-repeat is not updating when array is changed

前端 未结 3 2116
醉话见心
醉话见心 2020-12-20 03:17

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. Initiall

相关标签:
3条回答
  • 2020-12-20 03:55

    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;
    
    0 讨论(0)
  • 2020-12-20 04:07

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

    0 讨论(0)
  • 2020-12-20 04:17

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

    0 讨论(0)
提交回复
热议问题