码迷,mamicode.com
首页 > 其他好文 > 详细

ionic localstorage

时间:2014-10-13 13:29:29      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

 1 angular.module(‘locals‘,[])
 2 .factory(‘ls‘, [‘$window‘, function($window) {
 3   return {
 4     set: function(key, value) {
 5       $window.localStorage[key] = value;
 6     },
 7     get: function(key, defaultValue) {
 8       return $window.localStorage[key] || defaultValue;
 9     },
10     setObject: function(key, value) {
11       $window.localStorage[key] = JSON.stringify(value);
12     },
13     getObject: function(key) {
14       return JSON.parse($window.localStorage[key] || ‘{}‘);
15     }
16   }
17 }]);

把本地存儲定義成一個數據模型,方便下次的調用;

新建了一個 locals 模板

locals 創建了一個服務 ls

調用的時候 聲明locals模板 和注入 ls 服務

 1 angular.module(‘starter‘, [‘ionic‘,‘locals‘])
 2 
 3 .run(function($ionicPlatform,ls) {
 4   $ionicPlatform.ready(function() {
 5 
 6   ls.set(‘name‘, ‘test‘);
 7   console.log(ls.get(‘name‘));
 8   ls.setObject(‘info‘, {
 9     name: ‘Thoughts‘,
10     text: ‘Today was a good day‘
11   });
12 
13   var infos = ls.getObject(‘info‘);
14   console.log(infos);
15 
16     // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
17     // for form inputs)
18     if(window.cordova && window.cordova.plugins.Keyboard) {
19       cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
20     }
21     if(window.StatusBar) {
22       StatusBar.styleDefault();
23     }
24   });
25 });

 

ionic localstorage

标签:style   blog   color   io   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/xieyier/p/4021696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!