I am trying set up loading for my Angular app so that when somebody goes to page 2, page 3 will load in the background.
I am using $resource
to query Posts
with Post.query()
. Post.query({page: 1})
gets an array of post records with ID's 0-9.
My actual Post controller accepts parameters which specifies page: posts.json?page=1
where each page has 10 posts.
So Basically I want to query page 1 and page 2 on load and then concatenate them as:
$scope.visiblePosts
. When a user is on page 2 I then want page 3 to load in the background and to concatenate page 3 with $scope.visiblePosts
.
For pagination I am using the following code:
View:
<div ng-repeat="post in filtered = visiblePosts |
startFrom:(currentPage-1)*pageSize | limitTo:pageSize | orderBy:order:true">
App:
app.filter("startFrom", function() {
return function(input, start) {
if (input) {
start = +start;
return input.slice(start);
}
return [];
};
});
Controller:
$scope.currentPage = 1;
$scope.pageSize = 10;
$scope.noOfPages = Math.ceil($scope.posts.length / $scope.pageSize);
$scope.noPrev = function() {
return $scope.currentPage === 1;
};
$scope.noNext = function() {
return $scope.currentPage === $scope.noOfPages;
};
$scope.prevPage = function() {
return $scope.setPage($scope.currentPage - 1);
};
$scope.nextPage = function() {
return $scope.setPage($scope.currentPage + 1);
};
$scope.setPage = function(pageNo) {
return $scope.currentPage = pageNo;
};
$scope.filter = function() {
return window.setTimeout((function() {
return $scope.noOfPages = Math.ceil($scope.filtered.length / $scope.pageSize);
}), 10);
};
$scope.$watch("currentPage", $scope.setPage);
Any help is greatly appreciated, nothing seems to be working. I've tried concat() already and a few other things.