【发布时间】:2014-06-26 02:01:25
【问题描述】:
我在与不同控制器交互的多个视图上使用 angular-ui:bootstrap 分页指令;这些视图/控制器通常必须与排序和过滤进行交互,这需要动态更新页面大小,例如:
<pagination
on-select-page="setPage(page)"
items-per-page="pageSize"
boundary-links="true"
total-items="totalItems"
page="search.page"
max-size="maxVisiblePages"
class="pagination-sm"
num-pages="numPages"
previous-text="‹"
next-text="›"
first-text="«"
last-text="»"
>
</pagination>
在我的每个控制器中,我必须重新定义这些 $scope 函数,例如:
$scope.setPage = (page) ->
$timeout ->
$scope.search.page = page if $scope.search.page isnt page
$scope.totalItems = $scope.matchedItems.length
startIndex = (page - 1) * $scope.pageSize
$scope.images = $scope.matchedItems.slice(startIndex, startIndex + $scope.pageSize)
return
$scope.sortMatches = ->
$scope.matchedItems= $filter("orderBy")($scope.matchedItems, ['type','name'], true)
$scope.setPage 1
return
...
在避免控制器和视图中的代码重复的同时,为我提供这种排序/过滤/分页功能的最佳方式是什么?我在想我可以创建一个安装$scope 函数的类和另一个包装<pagination> 指令的指令,但我不知道这是否是最佳选择。
【问题讨论】:
标签: angularjs coffeescript angularjs-directive angular-ui-bootstrap