在离子框架中保存并加载应用程序

时间:2015-07-24 00:42:46

标签: android ios ionic-framework ionic application-icon

如何在关闭后保存应用程序的设置并在离子框架中再次打开时加载此保存,例如:保存复选框和列表然后再次加载它们 请帮帮我

1 个答案:

答案 0 :(得分:2)

将此服务添加到您的应用服务

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] || '{}');
    }
  }
}]);

要使用此服务,只需将$localstorage服务注入控制器或运行函数:

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

就是这样,现在当你重新打开应用程序时,你保存的所有东西都会存在

来源和更多here