Joseph Ocasio
Joseph Ocasio

Reputation: 999

Ionic local storage not persisting on device

I have a problem. When I run my app on chrome data is persistent due to the local storage of te web browser. However, when running on the simulator, if I kill the app, data gets wiped. I have read that localstorage it is a non volatile storage primary used for storing simple data. A DB is not needed for my app, local storage is more than enough. Any reason why this is happening?

angular.module('ionic.utils', [])

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

  }

}]);

Upvotes: 7

Views: 2746

Answers (2)

Anurag Pandey
Anurag Pandey

Reputation: 744

i am working on ionic and used localstorage(simple html local storage function)

  window.localStorage['Invite-Code'] = $scope.passCode;

above code set the value in local storage.

 var val = window.localStorage['Invite-Code'] || false;

above code show the value of localstorage value, if exist or not then its return value or false. here 'Invite-Code' is key

Upvotes: 1

Joseph Ocasio
Joseph Ocasio

Reputation: 999

Fixed it. It turns out everytime you want to run the app on the device you have to execute the command ionic build to get all the recent changes.

Upvotes: 0

Related Questions