Currently, I am facing an issue while using a JSON file to populate data in my Angular ng-table. Even though the JSON data is being displayed in a regular table format, certain functionalities of ng-table like pagination and view limit are not functioning as expected.
The error message appearing in the console is:
TypeError: Cannot read property 'slice' of undefined
Below are the relevant parts of my code:
var app = angular.module('app', ['ngResource', 'ngTable']);
app.service('tableService', ['$resource', function($resource){
this.getTableData = function(id, email, date) {
var tableDataList = $resource("table.json");
return tableDataList.get ({
id: id,
email: email,
date: date
})
}
}])
app.controller('tableCtrl', function($scope, tableService, ngTableParams) {
//Data
$scope.id = tableService.id;
$scope.email = tableService.email;
$scope.date = tableService.date;
$scope.dtResult = tableService.getTableData(this.id, this.email, this.date);
$scope.tableParams = new ngTableParams({
page: 1,
count: 10
}, {
total: $scope.dtResult.length,
getData: function ($defer, params) {
$defer.resolve($scope.dtResult.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
})
});
HTML
<table ng-table="tableParams" class="table">
<tr ng-repeat="w in dtResult.list">
<td data-title="'ID'">
{{ w.id }}
</td>
<td data-title="'Email Address'">
{{ w.email }}
</td>
<td data-title="'Date'">
{{ w.date }}
</td>
</tr>
</table>
You can access the Plunkr version here.
I would appreciate any assistance with resolving this issue. Thank you.