Elton Jamie
Elton Jamie

Reputation: 584

localstorage key undefined in angularjs

This is my service :

.factory('$localstorage', ['$window', function($window) {
  return {
    set: function(key, value) {
      $window.localStorage[key] = value;
    },
    get: function(key, defaultValue) {
      return $window.localStorage[key] || defaultValue;
    },
    setObject: function(key, value) {
      $window.localStorage[key] = JSON.stringify(value);
    },
    getObject: function(key) {
      return JSON.parse($window.localStorage[key] || '{}');
    }
  }
}]);

and I want to store my json into it so I do like below in my controller:

$localstorage.set(JSON.stringify($scope.data));

I use this extension https://chrome.google.com/webstore/detail/storage-area-explorer/ocfjjjjhkpapocigimmppepjgfdecjkb to check, I found that my value is undefined: enter image description here

..my json did passed through, but it goes into the key, not the value. Why?

Upvotes: 0

Views: 875

Answers (1)

dm03514
dm03514

Reputation: 55972

it looks like set has a key and a value, and you are just setting the first parameter which is a key?

$localstorage.set('yourKey', JSON.stringify($scope.data));

Upvotes: 2

Related Questions