Log Out

My routing app.confi...">

How to clear local storage after logout using angularjs

HTML code

<div><a href="#/logout"><h2>Log Out</h2></a></div> 

My routing

  app.config([ '$routeProvider', function($routeProvider) { $routeProvider.when('/', { templateUrl : 'app/components/login/Login.html', controller : 'loginCtrl' }).when('/register/', { templateUrl : 'app/components/register/register.html', controller : 'registerController' }).when('/welcome/', { templateUrl : 'app/components/welcome/welcome.html', controller : 'welcomeController' }).when('/home', { templateUrl : 'app/components/home/home.html', controller : 'homeController' }).when('/homeView', { templateUrl : 'app/components/home/homeView.html', controller : 'homeController' }).when('/graph', { templateUrl : 'app/components/home/graphView.html', controller : 'LineCtrl' }).when('/logout', { templateUrl : 'app/components/login/Login.html', controller : 'logoutCtrl' }).otherwise({ redirectTo : "/" }); } ]); 

Logout controller

 app.controller('LogoutController',function($location,$scope){ $location.path('/'); }); 

It works fine, and after logging out, it goes to the login page, and if I click the "Back" button, it will not be redirected to the last page, but I think that it’s not how I work, and I don’t clearing local storage during logout, I am stuck on how to clear local storage values ​​with logging out, please help me, I'm new to angularjs

input controller in which localstorage values ​​are set

  $scope.login = function () { $scope.empData=[] $scope.empData = loginService.get('http://183.1.1/HospitalManagementSystem/Service1.svc/LoginVerification/' + $scope.emailId + '/' + $scope.password); $scope.empData.then(function (empData) { console.log( $scope.empData) /*if (empData !== undefined && empData !== null){ if(empData._statusCode === constants.statusCode) {*/ if (empData.LoginVerificationResult.length === 0) { console.log('Employee details are not Available'); } else { $rootScope.name=empData.LoginVerificationResult[0].UserName; localStorage.setItem("Role ID",empData.LoginVerificationResult[0].RoleID); localStorage.setItem("UserId",empData.LoginVerificationResult[0].UserId); localStorage.setItem("UserName",empData.LoginVerificationResult[0].UserName); $location.path('/welcome') } }); }; 

LoginService

 app.service('loginService', [ '$http', function($http) { this.get = function(path) { var data = $http.get(path).then(function(response) { return response.data; }); return data; }; } ]); 
+8
angularjs
source share
1 answer

I think you only need localStorage.clear(); in the output controller:

 app.controller('LogoutController',function($location, $scope, $window){ $window.localStorage.clear(); $location.path('/'); }); 
+12
source share

All Articles