angularJS——利用theParam实现缓存功能
需求:列表页分页,跳转详情页时,保存当前页数和搜索关键词。从详情页返回时,回到之前页数并搜索同一关键词。
js:
mainApp.factory('theParam', function() { return { currentPage : 1,pageSize : 12,param:'' }; }); mainApp.controller('listController', function($scope, $http,theParam) { $scope.currentPage = theParam.currentPage; $scope.pageSize = theParam.pageSize; $scope.param = theParam.param; //搜索词 $scope.dataList; var dataObj = new Object(), reqUrl = "****"; var initObj = function(){ dataObj.pageNumber = $scope.currentPage; dataObj.pageSize = $scope.pageSize; dataObj.param = $scope.param; }
//*****省略其他****** //以跳转指定页数为例 $scope.goXpage = function(x) { initObj();
dataObj.pageNumber = x; theParam.param = $scope.param; //保存关键词 $http({ method : 'get', params : { param : $.toJSON(dataObj) }, data : {}, url : reqUrl }) .success(function(response, status, headers, config) {var rstate = response.result; if (rstate == "0") { var _data = response.message; $scope.dataList = _data.glist; $scope.currentPage = _data.pageNumber; theParam.currentPage = _data.pageNumber; //保存页数
}
else{ Showbo.Msg.alert('fail: '+response.error); } }) .error(function(response, status, headers, config) { Showbo.Msg.alert('error: '+response.error); }); }; }