您可以使用数组切片方法,因为您已经可以访问大数组,知道您所在的页码并知道每页的元素数量。您可以在下面的代码中重用 getPage 函数来实现这一点。
app.js
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.currentPage = 1;
$scope.pageSize = 5;
var meals = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
function getPage(currentPage, pageSize, arr, reverse) {
var beginIndex, endIndex, noOfPages;
if(reverse) {
beginIndex = arr.length - currentPage * pageSize;
} else {
beginIndex = currentPage * pageSize - pageSize;
}
endIndex = beginIndex + pageSize;
beginIndex = beginIndex < 0 ? 0 : beginIndex;
return arr.slice(beginIndex, endIndex);
}
//This will return the 5 elements in page 1 of meals array which will be meals 11 to 15 since the array is bound to the pagination directive in the reverse (desc) order
$scope.firstFiveArrRev = getPage($scope.currentPage, $scope.pageSize, meals, true);
//This will return the 5 elements in page 1 of meals array which will be meals 1 to 5 since the array is bound to the pagination directive in ascending order
$scope.firstFiveArr = getPage($scope.currentPage, $scope.pageSize, meals, false);
});
index.html
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.0-rc.0/angular.js" data-semver="1.4.0-rc.0"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
Displaying elements from page number {{currentPage}}
<br />
Page size is set to {{pageSize}}
<br />
When order is Reverse: true
<div>{{ firstFiveArrRev.toString() }}</div>
When order is Reverse: false
<div>{{ firstFiveArr.toString() }}</div>
</body>
</html>
这里是 plnkr
http://plnkr.co/edit/CgH1WFR1JvOLmQsacVoI?p=preview