I want to add the page
param in the query string. Using angular-ui pagination button the page 2 is correctly loaded, SearchService is called with the rights params, but immediately I see the page reloads with the $scope.currentPage=1.
So basically this code have a problem: I want to see page 2, I see SearchService called twice: the first time is right ($scope.currentPage=2), in the second one $scope.currentPage=1.
controller
app.controller('resultController', ["$scope","SearchService",
function($scope,SearchService){
$scope.query=$stateParams.query;
$scope.noresult=false;
$scope.currentPage = $stateParams.page || 1;
$scope.total = 0;
$scope.getData = function(page){
SearchService.simple($scope,$stateParams.query,page)
.then(function(res) {
if(res.data.total>0) {
$scope.result = res.data.docs;
$scope.total = res.data.total;
}else {
$scope.noresult = true;
}
});
}
};
$scope.getPage = function(page){
//---NOTE: state reload
$state.go('.', {page:page});
};
$scope.getData($scope.currentPage);
}]);
html
<div ng-repeat="e in result track by $index">
{{e.name}}
</div>
<div align="center">
<uib-pagination items-per-page="itemPerPage" num-pages="numPages"
total-items="total" boundary-link-numbers="true"
ng-model="currentPage" rotate="false"
class="pagination-sm" boundary-links="true"
ng-change="getPage(currentPage)">
</uib-pagination>
<div style="float: right; margin: 15px">
<pre>Page: {{currentPage}} / {{numPages}}</pre>
</div>
</div>
This could be a solution: https://github.com/begriffs/angular-paginate-anything