Undefined parameter in getData ng-table function

I have a problem with ng-table, where the parameters that should be passed to my getData function are undefined. I am new to AngularJS and ng-table, so any help would be appreciated. I checked that REST causes the below code to work, directly calling them, so the problem is somewhere in my angular code / configuration.

Anyway, here is a pseudo-example of my controller. The actual code is on the intranet, so I can’t insert it directly, so please forgive any typos from the transcription. Using ng-table 1.0.0 and angular 1.5.8:

myApp.controller('myCtrl', ['$scope', '$http', 'NgTableParams', function($scope, $http, NgTableParams) { $http.get('services/data/count').success(function(data) { // this works fine $scope.totalRows = data.rowCount; }); $scope.tableParams = new NgTableParams({ page: 1 count: 20 }, { total: $scope.totalRows, getData: function($defer, params) { // this line fails with params being undefined $http.get('/services/data/' + params.page() + '/' + params.count()) { .success(function(data) { $scope.data = data; $defer.resolve(data); }); } }); }]); 

And here is the corresponding html fragment:

 <table ng-table="tableParams" class="table table-condensed table-striped"> <tr ng-repeat="row in data"> // row stuff here </tr> </table> 

I added console.log statements before calling getData http, and the parameters print as undefined.

+5
source share
3 answers

Sorry, did not understand that my comment will be confused. Here is your answer:

It is assumed that the function entered in the getData key (using the NgTable API ) accepts only one argument, which represents params . In other words, the first argument to your getData function always contains params , even if you called it $defer . And the second argument is always undefined (the API calls it with only one argument), even if you named it params .

If you need access to $defer (and it seems like you are doing it), I think you should enter it in your controller (add '$defer' to your dependency array at the top, and then add $defer to the argument list of your controller function in the same position.)

It will look like this:

 myApp.controller('myCtrl', ['$scope', '$http', '$defer', 'NgTableParams', function($scope, $http, $defer, NgTableParams) { $http.get('services/data/count').success(function(data) { $scope.totalRows = data.rowCount; }); // ... getData: function(params) { $http.get('/services/data/' + params.page() + '/' + params.count()) { .success(function(data) { $scope.data = data; $defer.resolve(data); }); } 
+5
source

I also ran into this problem when updating an ng table. Your code should work in versions prior to version 1.0.0. 1.0.0-beta.9 is the latest version that supports your code.

Notes 1.0.0 say:

change signature getData strong>

The $ defer paramater parameter provided to your getData method has been removed. Instead, the getData method should return an array or promise that resolves the array.

To migrate

Earlier:

 var tp = new NgTableParams({}, { getData: getData }); function getData($defer, params){ // snip $defer.resolve(yourDataArray); } 

Now:

 var tp = new NgTableParams({}, { getData: getData }); function getData(params){ // snip return yourDataArrayOrPromise; } 
+5
source

The $ defer variable (thanks to Jesse Amano!) Had the values ​​I needed. I'm not sure why this works, but I can use it.

0
source

All Articles