Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to cache data you can do it in a number of ways. Two of approaches are here:-

  1. Use $http inbuild cache that's simple (details in link above).

    $http.get(url, { cache: true}).success(...);

  2. Use $cacheFactory (details in link above)

  3. Simply cache it inside your service:-

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);

    };
    }]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...