1
我需要在AngularJS應用程序中使用分頁幫助。使用分頁API動態調用數據
我的URL:localhost/myurl
給我數據:
Success: {
total: 349,
per_page: 10,
current_page: 1,
last_page: 35,
from: 1,
to: 10,
data: [{
name: 'name1',
age: 'age1'
}, {
name: 'name2',
age: 'age2'
}]
}
我的觀點:
<div ng-controller="DemoCtrl">
<p><strong>Page:</strong> {{tableParams.page()}}</p>
<p><strong>Count per page:</strong> {{tableParams.count()}}</p>
<table ng-table="tableParams" class="table">
<tr ng-repeat="user in $data">
<td data-title="'Name'">{{user.Name}}</td>
<td data-title="'Email'">{{user.EMAIL}}</td>
</tr>
</table>
</div>
我的js
var app = angular.module('main', ['ngTable'])
.service('myservice', ['$http', function($http){
this.getData = function(url){
return $http.get(url);
};
}])
.controller('DemoCtrl',['$scope', 'ngTableParams', 'myservice', function($scope, ngTableParams, myservice) {
myservice.getData('http://localhost/testpagination/public/testpagination').success(function(response){
if (response.Success) {
console.log(response.Success.data);
var data = response.Success.data;
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total: response.Success.total, // length of data
getData: function($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
} else if (response.Fail) {
console.log('i got fail.');
}
}).error(function(response){
console.log('http error');
});
}]);
我得到我的第一個十(1〜10)數據第一次,但我怎麼可以進入下一頁(調用第二分頁API)動態?
您是否使用$ routeProvider或$ stateProvider?根據它,你可以從URL中獲取頁面參數值,並將它傳遞給你的API! – 2014-10-19 18:42:57
用我的代碼更新了我的問題 – user284303 2014-10-19 18:56:39