最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ionic local storage remove item - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5

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 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);
   },
 };
}])
发布评论

评论列表(0)

  1. 暂无评论