How to save json response in local storage using angularjs?

前端 未结 6 601
野的像风
野的像风 2020-12-30 05:27

I have api which return json response and I want to store that json response in localstorage to use that response in my another html page using angularjs.

Here is my

相关标签:
6条回答
  • 2020-12-30 05:30

    I want to suggest this one because I used it and it works stable https://github.com/gsklee/ngStorage.

    After downloading and attaching it to your project you should add it as a dependency

        QAApp.controller('SearchCtrl', function ($scope, $http, $location,$localStorage) {
    
          $scope.search = function (searchtag) {
                   var request = $http({
                                  method: 'GET', 
                                  url: server + 'api/question/tagged/' + searchtag,
                                });
                        request.success(function(data, status, headers, config) {
                        $localStorage.qa = datal
                        $scope.qa = data;
                    });
    
    
    
      }
      }); 
    
    0 讨论(0)
  • 2020-12-30 05:32

    On your request.success(),use

    window.localStorage['storageName'] = angular.toJson(data);
    

    Then you can access the data in localstorage by

    var accessData = window.localStorage['storageName'];
    
    0 讨论(0)
  • 2020-12-30 05:33
    $scope.Save = angular.toJson(data); //Save to storage
    sessionStorage.setItem('blablabla',$scope.Save);
    localStorage.setItem('blablabla', $scope.Save); 
    
    $scope.DataFromJson = JSON.parse(sessionStorage["blablabla"]); //Get from storage
    $scope.DataFromJson = JSON.parse(localStorage["blablabla"]);
    
    0 讨论(0)
  • 2020-12-30 05:33

    /* To retrive json from localStorage */

    var user = angular.fromJson($window.localStorage['md-user']);
    

    /* To store json in loacalStorage */

    $window.localStorage['md-user'] = angular.toJson(user);
    
    0 讨论(0)
  • 2020-12-30 05:54

    I recommend using the angular-local-storage module on GitHub.

    0 讨论(0)
  • 2020-12-30 05:54

    To store

    $scope.storeItem = function() {
     sessionStorage.setItem('item', angular.toJson($scope.selectedItem));
    }
    

    To retrieve

    $scope.retrieve = function() {
        $scope.selectedItem = JSON.parse(sessionStorage.getItem('item'));
    }
    
    0 讨论(0)
提交回复
热议问题