Skip to content

Instantly share code, notes, and snippets.

@philjones88
Created June 7, 2013 11:06
Show Gist options
  • Save philjones88/5728559 to your computer and use it in GitHub Desktop.
Save philjones88/5728559 to your computer and use it in GitHub Desktop.
Paging in AngularJS
// Route, notice reloadOnSearch is set to false
.when('/enquiry', {
templateUrl: $base + 'Enquiry/list.cshtml', controller: EnquiryListController, reloadOnSearch: false, resolve: {
items: function ($route, enquiryService) {
var page = $route.current.params.page;
if (page == undefined) page = 1;
return enquiryService.list(page);
}
}
})
//Controller
function EnquiryListController($scope, $routeParams, $location, items, enquiryService) {
$scope.items = items.data.list;
$scope.totalPages = items.data.totalPages;
$scope.currentPage = items.data.currentPage;
$scope.service = enquiryService;
$scope.changed = function () {
$scope.$parent.isLoading = true;
$location.search({ "page": $scope.currentPage });
$scope.service.list($scope.currentPage).success(function (results) {
$scope.items = results.list;
$scope.totalPages = results.totalPages;
$scope.currentPage = results.currentPage;
$scope.$parent.isLoading = false;
});
};
$scope.page = function (newPage) {
$scope.currentPage = newPage;
$scope.changed();
};
}
// Service
app.factory('enquiryService', function ($http, $location, $route, $log) {
var url = "/admin/api/enquiry";
return {
url: url,
listUrl: listUrl,
listLocationUrl: listLocationUrl,
viewUrl: viewUrl,
list: function (page) {
$log.info("enquiryService.List : { " + page + " }");
return $http.get(url, {
params: {
page: page
}
});
}
};
});
// Inside view html
<a href='javascript:void(0)' ng-click="click(2)">Page 2</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment