14

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...

6 Answers 6

19

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'];
Sign up to request clarification or add additional context in comments.

6 Comments

Yes it is working I am able to save in local storage. Can you please tell me how can I use that "storageName" in html page to display the data using angularjs.
@AnitaMehta var accessData = window.localStorage['storageName']; $scope.VarName = angular.fromJson(accessData); so you can use the 'varname' to customize your data in HTML pages
I got the response but I am not able to display.....what I am doing is on onclick I got the response and want to display to next page.....So after onclcik I got the data and also going to next page the data is not displaying....
var accessData = window.localStorage['storageName']; $scope.VarName = angular.fromJson(accessData); In HTML Page use {{VarName.<JSON string Name of Name_value_Pair>}}
use double curly braces like {{<JSON data>}}, to display data in HTML pages
|
5

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



  }
  }); 

Comments

4
$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"]);

Comments

3

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

Comments

2

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

Comments

0

To store

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

To retrieve

$scope.retrieve = function() {
    $scope.selectedItem = JSON.parse(sessionStorage.getItem('item'));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.