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?
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.
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.
Ionic Local Storage uses the local storage system of browser for key/value pairs storing. The limit of Local Storage is only 5MB.
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).
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.
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();
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With