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.
You 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 complete the whole transaction of the removal of your key:value
from LocalStorage
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);
},
};
}])