Can someone help me to create a method to remove from ionic local storage?
So far I have tried
set: function(key, value) {
$window.localStorage[key] = value;
},
get: function(key) {
return $window.localStorage[key];
},
setObject: function(key, value) {
$window.localStorage[key] = JSON.stringify(value);
},
getObject: function(key) {
return JSON.parse($window.localStorage[key]);
},
removeItem: function(key){
$window.localstorage.splice(key, 1);
}
removeItem doesnt work at all. I want to remove by positions, not by key.
Can someone help me to create a method to remove from ionic local storage?
So far I have tried
set: function(key, value) {
$window.localStorage[key] = value;
},
get: function(key) {
return $window.localStorage[key];
},
setObject: function(key, value) {
$window.localStorage[key] = JSON.stringify(value);
},
getObject: function(key) {
return JSON.parse($window.localStorage[key]);
},
removeItem: function(key){
$window.localstorage.splice(key, 1);
}
removeItem doesnt work at all. I want to remove by positions, not by key.
Share Improve this question edited May 19, 2015 at 11:00 LeftyX 35.6k21 gold badges137 silver badges197 bronze badges asked May 19, 2015 at 2:32 Joseph OcasioJoseph Ocasio 9994 gold badges10 silver badges19 bronze badges 1-
Just use
$window.localStorage.removeItem(key)
– devqon Commented May 19, 2015 at 11:03
3 Answers
Reset to default 5You are using localStorage
as an array, while it isn't. It has default functions to remove an item:
removeItem: function(key){
$window.localStorage.removeItem(key);
}
If you want to remove by index, you have to get the item first:
removeByIndex: function (index) {
$window.localStorage.removeItem($window.localStorage.key(index));
}
Try the built in methods, which will help to plete the whole transaction of the removal of your key:value
from LocalStorage
https://auth0./docs/native-platforms/ionic#8
This would be the best way. With this factory you can create, retrieve, or delete any created key
.factory('sessionService',['$http',function($http){
return {
set:function(key,value){
return localStorage.setItem(key,JSON.stringify(value));
},
get:function(key){
return JSON.parse(localStorage.getItem(key));
},
destroy:function(key){
return localStorage.removeItem(key);
},
};
}])