如何存储在angularjs数据分页并将其作为响应 [英] How to store data in angularjs for pagination and send it as response
问题描述
我的要求是我要存储大量的采用了棱角分明的分页数据。这个数据将来自服务器端。我们怎样才能做到这一点采用了棱角分明?
先谢谢了。
My requirement is I have to store large amount of data using angular for pagination. This data will come from server side. How can we do it using angular?. Thanks in advance.
推荐答案
如果您想缓存数据,您可以通过多种方式做到这一点。办法有两个是<一个href=\"http://stackoverflow.com/questions/14117653/how-to-cache-an-http-get-service-in-angularjs\">here:-
If you want to cache data you can do it in a number of ways. Two of approaches are here:-
-
使用的$ HTTP inbuild缓存在<一个简单的(细节href=\"http://stackoverflow.com/questions/14117653/how-to-cache-an-http-get-service-in-angularjs\">link以上)。
$ http.get(URL,{缓存:真})。成功(...);
$http.get(url, { cache: true}).success(...);
在 cacheFactory (详细信息href=\"http://stackoverflow.com/questions/14117653/how-to-cache-an-http-get-service-in-angularjs\">link以上)
Use $cacheFactory (details in link above)
只要缓存它的服务中: -
Simply cache it inside your service:-
服务: -
var todoApp = angular.module("todoApp",[]);
todoApp.factory('dbService', ['$q','$http',function ($q , $http) {
var service ={};
service.localCache = {hasdata:false,data:{},lastLoaded:new Date()};
service.getUrl = function (urlToGet,burstCache) {
var svc=this;
var deferred = $q.defer();
if ((!burstCache)&&(svc.localCache)&&(svc.localCache.hasdata)) {
console.log('resolve from local cache');
return(svc.localCache.data);
}else{
var responsePromise = $http.get(urlToGet);
responsePromise.success(function (data, status, headers, config) {
svc.localCache={};
svc.localCache.hasdata=true;
svc.localCache.data=data;
svc.localCache.lastLoaded= new Date();
deferred.resolve(data);
console.log('resolve from ajax') });
responsePromise.error(function (data, status, headers, config) {
deferred.reject({ error: "Ajax Failed", errorInfo: data }); svc.localCache={}; });
}
return (deferred.promise);
}
return service;
}]);
控制器: -
Controller:-
todoApp.controller("ToDoCtrl",['$scope','$timeout','dbService',function($scope, $timeout, dbService)
{
$scope.todo={};
//Fetches the data from server. 'true' means burstCache
$timeout(function(){
dbService.getUrl('/api/userdata',true).then(function(resp){
$scope.todo=resp;
});},1);
//ReLoads the data from cache
$scope.reLoad=function(){
$scope.todo={};
$timeout(function() {$scope.todo=dbService.getUrl('/api/userdata');},1000);
};
}]);
这篇关于如何存储在angularjs数据分页并将其作为响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!