ionic localstorage使用例子

来源:互联网 发布:电脑mac地址查询方法 编辑:程序博客网 时间:2024/05/19 14:36

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

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


1 0
原创粉丝点击