ionic angularjs 本地数据存储localstorage

存储方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//本地存储数据
.factory('locals',['$window',function($window){
return{
//存储单个属性
set :function(key,value){
$window.localStorage[key]=value;
},
//读取单个属性
get:function(key,defaultValue){
return $window.localStorage[key] || defaultValue;
},
//存储对象,以JSON格式存储
setObject:function(key,value){
$window.localStorage[key]=JSON.stringify(value);
},
//读取对象
getObject: function (key) {
return JSON.parse($window.localStorage[key] || '{}');
}
}
}]);

controller调用

1
2
3
4
5
6
7
8
添加对象 locals
.controller('loginController',function($scope, $ionicPopup, $state,locals){
使用存储或读取方法
//存储数据
locals.set("username",user.username);
locals.set("password",user.password);
//读取数据
console.log(locals.get("username",""));

标签: