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 模板

locals 創建了一個服務 ls

調用的時候 聲明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();
}
});
});
上一篇:c++对象模型之Data布局


下一篇:【转载】C++ function、bind和lambda表达式