这是angularjs中正确的分页方式吗?

时间:2015-10-26 18:02:33

标签: angularjs

点击"更多"我调用调用loadNextPage方法的loadComments方法,该方法根据下一个标识nextCommentId加载数据并将数据推送到allComments数组,此数组以html呈现

$scope.loadNextPage = function()
{
    loadComments($scope.nextCommentId);
}

function loadComments(page){
    $scope.allComments.push(d.data);
    ..
    ..
    $scope.nextCommentId = nextId;
}

这是我的HTML

<div ng-repeat="comment in allComments">
 ...
</div>

我的问题是,这是在angularjs中进行分页的正确方法,因为我将所有数据都保存在数组中,因此如果数据增长会占用内存。

1 个答案:

答案 0 :(得分:0)

    <div data-pagination="" data-num-pages="numPages()" 
  data-current-page="currentPage" data-max-size="maxSize"  
  data-boundary-links="true"></div>

todos.controller("TodoController", function($scope) {
   $scope.filteredTodos = []
  ,$scope.currentPage = 1
  ,$scope.numPerPage = 10
  ,$scope.maxSize = 5;

  $scope.makeTodos = function() {
    $scope.todos = [];
    for (i=1;i<=1000;i++) {
      $scope.todos.push({ text:"todo "+i, done:false});
    }
  };
  $scope.makeTodos(); 

  $scope.numPages = function () {
    return Math.ceil($scope.todos.length / $scope.numPerPage);
  };

  $scope.$watch("currentPage + numPerPage", function() {
    var begin = (($scope.currentPage - 1) * $scope.numPerPage)
    , end = begin + $scope.numPerPage;

    $scope.filteredTodos = $scope.todos.slice(begin, end);
  });
});