javascript - Angularjs localstorage with object -
i need store json object angularjs local storage. in case when ever push notification receive gcm need store local storage object , need display in 1 separate page, have done below code
var sessionmessages = []; var notification = { "local": "bk3rnwte3h0:ci2k_hhwgipodkcizvvdmexudfq3p1...", "ldata": { "nick": "mario", "body": "great match!", "room": "portugalvsdenmark" } }; sessionmessages.push(notification); var notification1 = { "server": "bk3rnwte3h0:ci2k_hhwgipodkcizvvdmexudfq3p1...", "sdata": { "nick": "mario", "body": "great match!", "room": "portugalvsdenmark" } }; sessionmessages.push(notification1); $localstorage.set('mrksesionnotifi', sessionmessages); console.log($localstorage.getobject('mrksesionnotifi')); // error uncaught syntaxerror: unexpected token o in json @ position 1
my factory below,
.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] || '{}'); } } }])
but when store object array localstorage not it, getting error (uncaught syntaxerror: unexpected token o in json @ position 1). please expected result.
var sessionmessages = []; var notification = { "local": "bk3rnwte3h0:ci2k_hhwgipodkcizvvdmexudfq3p1...", "ldata": { "nick": "mario", "body": "great match!", "room": "portugalvsdenmark" } }; sessionmessages.push(notification); var notification1 = { "server": "bk3rnwte3h0:ci2k_hhwgipodkcizvvdmexudfq3p1...", "sdata": { "nick": "mario", "body": "great match!", "room": "portugalvsdenmark" } }; sessionmessages.push(notification1); $localstorage.setitem('mrksesionnotifi', sessionmessages); console.log($localstorage.getitem('mrksesionnotifi'));
try code
Comments
Post a Comment