Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-table , getData called more than once, why?

For some reason when getData uses angular resource to bring the data it is being called twice, causing the resource to do it REST request twice too <--- bad...

Any idea why and how to solve it?

Here a working testcase/plunker example that recreates this scenario (look at the browser console - "getData being called...." displayed twice ) b.t.w as you can see I'm not really using the resource to bring real data, just to demonstrate the scenario, In my real app I do use the resource to bring real data and its being called twice just like in this example,

Thanks ahead


After looking into the src of the ng-table I noticed the following

$scope.$watch('params.$params', function(params) {
    $scope.params.settings().$scope = $scope;
    $scope.params.reload();
}, true);

Which means that the tables calls it 'getData' on count/filter/group/groupBy/page/sorting which explains the behavior I was seeing.

like image 774
Daniel Avatar asked Dec 19 '22 17:12

Daniel


1 Answers

When you call params.count(...) you ask ng-table to refresh data as you change page size. That's why you have two get-data calls.

If you don't want to have paging, then remove calls params.count and params.total. If you need paging, then set page size and do not change it in getData.

like image 153
Alexander Vasilyev Avatar answered Jan 04 '23 22:01

Alexander Vasilyev