back button behavior in angular js application

烈酒焚心 提交于 2019-12-25 07:01:18

问题


I have an issue with a SPA app written in angularJS. The application consist of thre views - login view, main view and log out view.

  • myapp/#/login
  • myapp/#/main
  • myapp/#/logout

my route provider:

function Router($routeProvider) {
    $routeProvider
        .when('/login', {
            templateUrl: 'app/components/login/login.tmpl.html',
            controller: 'login.ctrl',
            controllerAs: 'vm'
        })
        .when('/main', {
            templateUrl: 'app/components/dashboard/main.tmpl.html',
            controller: 'dashboard.ctrl',
            controllerAs: 'vm'
        })
        .when('/logout', {
            templateUrl: 'app/components/logout/logout.tmpl.html',
            controller: 'logout.ctrl',
            controllerAs: 'vm'
        })
        .otherwise({
            redirectTo: '/login'
        });
}

Use case: 1) user logs in. 2) user sees the content in the main view 3) user logs out. 4) user is redirected to the logout view for confirmation 5) user is redirected to the login view

If I press the back browser button after step 5 I am redirected to the main view. Is there a way to change the behavior of the back button when I am in the login view? I believe it is something simple and I apologize if the question is duplicated. Thank you for your time and responses!


回答1:


Please see working example: http://plnkr.co/edit/46O0znC5HFDE4cYXSm5h?p=preview

Stored data in cookies in login function as follows,

$cookies.userinfo = {
    'id': 1,
    'name': 'pqr'
};

And on logout remove that data -

delete $cookies.userinfo;

then check for 'angular.isDefined($cookies.userinfo)' (userinfo is cookie name which given at the time of storing data in it) if find then redirect it to your page which you want to see after login. i.e

app.run(function ($cookies) {
      $rootScope.$on("$routeChangeStart", function () {
        if (angular.isDefined($cookies.userinfo)) {
            $location.path("/pathname");
        }

      });
});


来源:https://stackoverflow.com/questions/30376568/back-button-behavior-in-angular-js-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!