【发布时间】:2018-06-04 23:44:08
【问题描述】:
我正在使用 AngularJS v1.6.6 和 angular-ui-bootstrap 版本:2.5.0 创建使用 ng-repeat 指令构建的分页列表。
分页有效,但每次页面更改 $index 变量计数重新开始时,我需要将此值作为参数传递给函数,因此,例如,如果我单击第二页的第一项,我需要传递该值10.
看到这个小提琴:http://jsfiddle.net/elenat82/vLfLtrxc/
HTML:
<div ng-app="myApp">
<div ng-controller="someController">
<div>list.length = {{list.length}}</div>
<div>currentPage = {{currentPage}}</div>
<div>itemPage = {{itemPage}}</div>
<div>maxSize = {{maxSize}}</div>
<br><br><br><br>
<table>
<tbody>
<tr ng-repeat="item in list | pagination:(currentPage-1)*itemPage | limitTo:itemPage track by $index">
<td>{{ $index +1 }}</td>
<td>{{ item.task }}</td>
</tr>
</tbody>
</table>
<div>
<ul uib-pagination
total-items="list.length" ng-model="currentPage" max-size="maxSize" boundary-links="true" boundary-link-numbers="true" force-ellipses="true" items-per-page="itemPage" first-text="First" last-text="Last" next-text="Next" previous-text="Prev">
</ul>
</div>
JS:
var app = angular.module('myApp', ['ui.bootstrap']);
app.controller('someController', function($scope, $filter) {
$scope.list = [
{task: "task n. 1"},
{task: "task n. 2"},
{task: "task n. 3"},
{task: "task n. 4"},
{task: "task n. 5"},
{task: "task n. 6"},
{task: "task n. 7"},
{task: "task n. 8"},
{task: "task n. 9"},
{task: "task n. 10"},
{task: "task n. 11"},
{task: "task n. 12"},
{task: "task n. 13"},
{task: "task n. 14"},
{task: "task n. 15"},
{task: "task n. 16"},
{task: "task n. 17"},
{task: "task n. 18"},
{task: "task n. 19"},
{task: "task n. 20"}
];
$scope.currentPage = 1;
$scope.itemPage = 5;
$scope.maxSize = 5;
});
app.filter('pagination', function () {
return function (input, start) {
start = +start;
if (input) {
return input.slice(start);
}
else {
return input;
}
};
});
【问题讨论】:
-
以下答案是正确的。请在下面选择这个。
标签: angularjs angular-ui-bootstrap