7

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?

arun kamboj
  • 1,145
  • 4
  • 17
  • 48

2 Answers2

6

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.

Hitu Bansal
  • 2,917
  • 10
  • 52
  • 87
4

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();
 };
Community
  • 1
  • 1
manzapanza
  • 6,087
  • 4
  • 39
  • 48