Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save json response in local storage using angularjs?

Tags:

json

angularjs

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 code which return json response....

QAApp.controller('SearchCtrl', function ($scope, $http, $location) {

  $scope.search = function (searchtag) {
           var request = $http({
                          method: 'GET', 
                          url: server + 'api/question/tagged/' + searchtag,
                        });
                request.success(function(data, status, headers, config) {
                console.log(data);
                $scope.qa = data;
            });

        }
  }); 

Please tell me how can I store it...

like image 352
Anita Mehta Avatar asked Aug 20 '14 06:08

Anita Mehta


2 Answers

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'];
like image 149
Rajesh Manilal Avatar answered Oct 22 '22 03:10

Rajesh Manilal


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



  }
  }); 
like image 25
Narek Mamikonyan Avatar answered Oct 22 '22 03:10

Narek Mamikonyan