简体   繁体   中英

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

  }

}]);

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM