【发布时间】:2017-11-06 23:29:29
【问题描述】:
下面是我使用 angularjs-ui-bootstrap 进行简单分页的代码。由于我将使用太大的数据集以使其页面客户端我只是在页面更改时抓取每页的数据。
我遇到的问题是当页面重新加载时,分页总是重置到第一页。重新加载页面时如何让它留在当前页面上?
html
<uib-pagination total-items="totalCount" ng-model="currentPage" ng-change="pageChanged()" items-per-page="2"></uib-pagination>
javascript
$scope.currentPage = $location.search().page;
getData($scope.currentPage);
$scope.pageChanged = function () {
getData($scope.currentPage);
$location.url('?page=' + $scope.currentPage);
};
function getData(pageNumber) {
if (pageNumber == null)
pageNumber = 1;
$http.get('http://localhost.angulartestapi.com/v1/AngularTestApi/getteams?pageNumber=' + pageNumber)
.success(function (data) {
$scope.teams = data.Teams;
$scope.totalCount = data.TotalCount;
}).error(function () {
alert("error");
});
此代码当前在 url 中添加了一个 ?page={currentPage} 参数。当页面重新加载时,它会从$location.search().page 获取保存的页面进入getData(),并将$scope.currentPage 设置为正确的页码。但随后它进入pageChanged(),$scope.currentPage 重置回1。
编辑: Cookie 方法
所以我尝试将 currentPage 保存在这样的 cookie 中:
$scope.currentPage = $cookies.get('page');
getData($scope.currentPage);
$scope.pageChanged = function () {
getData($scope.currentPage);
$cookies.put('page', $scope.currentPage);
};
当页面重新加载时,它会从 cookie 中获取保存的页面并进入 getData(),并将 $scope.currentPage 设置为正确的页码。但随后它进入pageChanged(),$scope.currentPage 重置回1。所以问题仍然存在。
编辑 2:
即使$scope.currentPage 被硬编码为2,当页面重新加载时$scope.currentPage 在$scope.pageChanged 中被重置为1。
$scope.currentPage = 2;
getData($scope.currentPage);
$scope.pageChanged = function () {
getData($scope.currentPage);
};
最终编辑:
找到了留在当前页面的方法。答案如下。
【问题讨论】:
-
refresh是指重新加载吗?角度模型将在页面重新加载时重新初始化。 -
@NarainMittal 是的,当页面重新加载时。有没有办法重新加载并停留在当前页面?
标签: angularjs angular-ui-bootstrap