LocalStorage Persistence


Hi guys,

I have problems with the LocalStorage. I just want to store some favorites. Here’s my code:

.factory('Database', [function(){
var myFactory = {};

//local storage
var ls = window.localStorage;
var db = {};

db.addFavorite = function (id){

db.removeFavorite = function (id){

db.isFavorite = function (id){
  var bool = ls.getItem(id);
  if (bool != true) {bool = false};
  return bool;

return myFactory;

Everything works fine (especially in Chrome), but not on device (Galaxy S3). When the app is active, it works, but when I close it via taskmanager, the LocalStorage is cleared.

Any ideas?

Thanks for your help.

P.S.: I tried cookies, LocalStorage, PhoneGap Storage and the ngCordova SQLite plugin (https://github.com/driftyco/ng-cordova). Nothing worked.


You need to stringify json and then parse it. e.g.

localStorage.setItem('favorite', JSON.stringify(data));

$scope.favorite = JSON.parse(localStorage.getItem("favorite"))


that’s it! Thanks so much!


Just for people finding this post it’s better to use angular.toJSON() because it takes care of removing all $*** properties that could have been added by angular like with the ng-repeat directive.