把本地儲存定義成組資料模型
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(); } });});