ionic localstorage使用例子

把本地储存定义成组数据模型

angular.module('locals',[])
.factory('ls', ['$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] || '{}');
     }
   }
 }]);

locals 模型上创建了一个ls的服务。

使用时需要引入

angular.module('starter', ['ionic','locals'])

.run(function($ionicPlatform,ls) {
  $ionicPlatform.ready(function() {

  ls.set('name', 'test');
  console.log(ls.get('name'));
  ls.setObject('info', {
    name: 'Thoughts',
    text: 'Today was a good day'
  });

  var infos = ls.getObject('info');
  console.log(infos);

    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
});


你可能感兴趣的:(Ionic)