我遇到了同样的问题,但这里的答案没有满足我的需求(或者可能想要)。我决定自己解决这个问题,并决定创建一个过滤器,其唯一目的是在给定目标的给定属性中记录通过它的项目。我想出了这个:
/**
* Author: Eric Ferreira <http://stackoverflow.com/users/2954747/eric-ferreira> ©2015
*
* This filter will sit in the filter sequence, and its sole purpose is to record
* the current contents to the given property on the target object. It is sort of
* like the 'tee' command in *nix cli.
*/
angular.module('app').filter('record', function() {
return function(array, property, target) {
if (target && property) {
target[property] = array;
}
return array;
}
});
然后,您在分页中使用过滤器(或者您想要实际获取当前数组的任何地方[想想,在使用搜索查询过滤之后、分页之后、过滤当前页面之后等等]),如下所示:
<div dir-paginate="item in items | itemsPerPage:pageSize | record:'currentPage':this">{{item.text}}</div>
您还可以在一个序列中多次使用它:
<div dir-paginate="item in items | filter:searchQuery | record:'filtered':this | itemsPerPage:pageSize | record:'currentPage':this">{{item.text}}</div>
上面将记录当前页面和当前过滤查询结果的所有记录。
这将记录(并在更改时更新)当前页面$scope.currentPage
. The this
上例中是过滤器的目标。它决定$scope.this
对于大多数意图和目的来说,这只是$scope
.
在您的特定情况下,您可以使用此行(在模块中添加/需要过滤器之后)来代替分页:
<li dir-paginate="meal in perman = ( meals | filter:q ) | orderBy: order?'key':'-key' | itemsPerPage: pageSize | record:'currentPage':this">{{ meal.key + ': ' +meal.val }}</li>
我继续分叉你的插件来展示它也可以工作:
http://plnkr.co/edit/uC3RiC?p=preview