这是我的代码.
$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(); };
我想从我的localStorage中删除access-token变量,当我使用它时它适用于浏览器
$window.localStorage.clear(); $window.localStorage.removeItem(access-token);
但它不适用于我的应用程序.
这是我的本地存储工厂
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] || '{}'); } } }]);
任何的想法?
我平均也遇到过同样的问题.我正在使用一些全局变量用于本地存储.
你的代码似乎没问题.但是你可以更好地优化它
原文链接:https://www.f2er.com/angularjs/141550.html你的代码似乎没问题.但是你可以更好地优化它
$scope.logout = function(){ $window.localStorage.clear(); $ionicHistory.clearCache(); $ionicHistory.clearHistory(); };
你不需要设置
localstorage.set('user_id',""); localstorage.set('access-token',""); localstorage.set('isUserTraverseColony',0); localstorage.set('isStarted',0);
因为你正在清理他们.