11

I am trying to create a route that clears the users session and redirects them back to the root homepage.

.config(function config( $routeProvider, $stateProvider ) {

    $routeProvider.
        when('/logout', {resolve: {redirect: function(Session){
            Session.clear();
            return "/home";
        }}});

I'm obviously doing something wrong here, as calling

$location.path("/logout");

... ignores the function and redirects back to the default route. I've tried adding console.log statements to the function to see if it is being called.

Am I using the redirect function incorrectly?

4

4 回答 4

19

您是否考虑过将注销逻辑放在单独的控制器中?会更干净,更健壮,并使重定向更直接。像这样:

function LogoutController($location) {
    Session.clear();
    $location.path('/home');
}

你的路线是:

when('/logout', {
  template: '', //A template or templateUrl is required by AngularJS, even if your controller always redirects.
  controller: 'LogoutController'
}).    
于 2013-10-28T16:36:26.430 回答
7

我遇到了同样的问题,我所做的是在我的 navigationController 中创建一个注销函数,当点击 URL 时它会被命中

 <li><a href="" ng-click="logout()" target="_self">Log Out</a></li>

在我的导航控制器中:

$scope.logout = function () {
                localStorage.clearAll();
                window.location = '/logout';
            };

我在 Angular 后面运行 ASP.NET,所以我需要浏览器(不是 Angular)路由到在 ASP.NET 配置中映射的 /logout(进行一些其他会话清理并重定向到身份验证应用程序)

希望这可以帮助

于 2014-10-15T15:21:04.410 回答
0

只需存储 $sessionStorage (username) 然后删除 $sessionStorage (username) ..

         $scope.logout = function(){
         delete $sessionStorage.sessname; //sessname is get sessionStorage username 
           $location.path('/login');
                     };

帮助我获取此链接:https ://stackoverflow.com/questions/36056745/angularjs-click-logout-button-to-clear-sessionstorage-again-and-again-go-back-to

于 2016-03-17T11:39:21.207 回答
0

我使用这种方法

$routeProvider
      .when('/logout', {
            resolve: {
                logout: ['authService', function (authService) {
                    authService.clear(true);
                }]
            }
        })
于 2018-06-05T13:29:59.527 回答