Ali Karimi
Ali Karimi

Reputation: 29

Save and load app in ionic framework

How do I save setting of app after close it and load this save when i open it again in ionic framework , for example : save checkbox and list then load them again please help me

Upvotes: 0

Views: 170

Answers (1)

Tenoch G
Tenoch G

Reputation: 171

Add this service to your app services

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

.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 to use this service, just inject the $localstorage service into a controller or run function:

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

.run(function($localstorage) {

  $localstorage.set('name', 'Max');
  console.log($localstorage.get('name'));
  $localstorage.setObject('post', {
    name: 'Thoughts',
    text: 'Today was a good day'
  });

  var post = $localstorage.getObject('post');
  console.log(post);
});

That's it, now everything you save will be there when you reopen the app

source and more here

Upvotes: 2

Related Questions