I implemented this code for the controller
.controller('tableCtrl', function($scope, $filter, $sce, ngTableParams, tableListUser) {
var data = [];
tableListUser.getAll()
.then(function(data) {
console.log("THEN DATA : ", data);
$scope.data = data;
});
data = $scope.data;
console.log("DATA : ", data);
// Full Params Sort, Filter, Edit
this.tableFull = new ngTableParams({
page: 1, // show first page
count: 10, // count per page
sorting: {
name: 'asc' // initial sorting
}
}, {
total: data.length, // length of data
getData: function ($defer, params) {
console.log("Data tableFull :", $scope.data);
// use build-in angular filter
var orderedData = params.sorting() ? $filter('orderBy')($scope.data, params.orderBy()) : $scope.data;
orderedData = params.filter() ? $filter('filter')(orderedData, params.filter()) : orderedData;
orderedData = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());
console.log(orderedData.length);
params.total(data.length); // set total for recalc pagination
$defer.resolve(orderedData);
}
})
}
And this for the service
.service('tableListUser', function($http, SessionService, AccessLevels) {
return {
getAll: function() {
console.log('Service tableListUser');
var token = SessionService.get('token');
console.log(token);
$http.defaults.headers.common.Authorization = 'Bearer ' + token;
var url = 'http://api-ec.local/users';
var promise = $http.get(url);
return promise.then(function(result) {
console.log("RESULT : ", result);
return result.data.data;
});
}
};
})
I review many post here like this : asynchronously populating an AngularJS ngTable with json data
But I can't use the data in the var data
because it is Undefined
. The response of the promise is good and get the data that the app expect because when I log the data in tableListUser.getAll().then
, it its well. Is there a problem with way I use $socope.data
to pass the data and use it in ngTableParams
. This are blocking me alot.
Thanks in advance for your help.