Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear localstorage data in ionic?

Here is my code.

$scope.logout=function(){
    localstorage.set('user_id', "");
    localstorage.set('access-token', "");
    localstorage.set('isUserTraverseColony', 0);
    localstorage.set('isStarted', 0);
    $window.localStorage.clear();
    $window.localStorage.removeItem(access-token);
    $ionicHistory.clearCache();
    $ionicHistory.clearHistory();
 };

I want to delete the access-token variable from my localStorage, It working fine for browser when i use

 $window.localStorage.clear();
 $window.localStorage.removeItem(access-token);

But its not working for my App.

here is my localstorage factory

angular.module('starter.controllers').factory('localstorage', ['$window', '$localStorage','$q', function ($window, $localStorage,$q) {
    return {
        set: function (key, value) {
             var deferred = $q.defer();
            $window.localStorage[key] = value;
                 deferred.resolve(1);
                 return deferred.promise;
        },
        get: function (key, defaultValue) {
            return $window.localStorage[key] || defaultValue;
        },
        setObject: function (key, value) {
            $window.localStorage[key] = JSON.stringify(value);
        },
        getObject: function (key) {
            return JSON.parse($window.localStorage[key] || '{}');
        }
    }

}]);

Any Idea?

like image 349
arun kamboj Avatar asked Aug 03 '15 11:08

arun kamboj


People also ask

How do I clear local storage value?

To delete local storage sessions, use the removeItem() method. When passed a key name, the removeItem() method removes that key from the storage if it exists. If there is no item associated with the given key, this method will do nothing.

What does localStorage Clear () do?

The clear() method removes all the Storage Object item for this domain. The clear() method belongs to the Storage Object, which can be either a localStorage object or a sessionStorrage object.

What is the limit of local storage in ionic?

Ionic Local Storage uses the local storage system of browser for key/value pairs storing. The limit of Local Storage is only 5MB.

What is local storage ionic?

The LocalStorage storage engine uses the browser's local storage system for storing key/value pairs. Note: LocalStorage should ONLY be used for temporary data that you can afford to lose. Given disk space constraints on a mobile device, local storage might be "cleaned up" by the operating system (iOS).


2 Answers

I had also faced same problem mean time ago. I was using some Global variable for Local Storage. Your code seems ok. But you can optimize it better

$scope.logout = function(){
  $window.localStorage.clear();
    $ionicHistory.clearCache();
    $ionicHistory.clearHistory();
 };

You don't need to set

localstorage.set('user_id', "");
localstorage.set('access-token', "");
localstorage.set('isUserTraverseColony', 0);
localstorage.set('isStarted', 0);

because you are clearing them.

like image 154
Hitu Bansal Avatar answered Sep 27 '22 18:09

Hitu Bansal


You could add the clear() method to your custom localstorage service:

angular.module('starter.controllers').factory('localstorage', ['$window', '$localStorage','$q', function ($window, $localStorage,$q) {
    return {
        set: function (key, value) {
            $window.localStorage.setItem(key, value);
        },
        get: function (key, defaultValue) {
            return $window.localStorage.getItem(key) || defaultValue;
        },
        setObject: function (key, value) {
            $window.localStorage.setItem(key, JSON.stringify(value));
        },
        getObject: function (key) {
            return JSON.parse($window.localStorage.getItem(key) || '{}');
        },
        clear: function () {
            $window.localStorage.clear();
        }
    }

}]);

Note: I updated your service removing promise because HTML5 localStorage is syncronous and using standard get and set methods

And then in you controller:

$scope.logout = function(){
    localstorage.clear();
    $ionicHistory.clearCache();
    $ionicHistory.clearHistory();
 };
like image 31
manzapanza Avatar answered Sep 27 '22 18:09

manzapanza