AngularJS登录后如何重定向到下一个路由

前端之家收集整理的这篇文章主要介绍了AngularJS登录后如何重定向到下一个路由前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用$ rootScope。$ on(‘$ routeChangeStart’,function(event,next,current),如果路由需要身份验证,我将重定向登录页面

登录后如何重定向回目的路线?

这是对我有用的简化版本:
app = angular.module('ngApp',[]).config(function ($routeProvider) {
  $routeProvider
    .when('/dashboard',{
        templateUrl: 'dashboard.html',controller: 'dashboardController',loginrequired: true //
      })
    .when('/login',{
        templateUrl: 'login.html',controller: 'loginController'
      })
    .otherwise({redirectTo: '/login'})
});

然后在应用程序的运行块中:

app.run(function ($location,$rootScope) {
    var postLogInRoute;

    $rootScope.$on('$routeChangeStart',function (event,nextRoute,currentRoute) {

    //if login required and you're logged out,capture the current path
        if (nextRoute.loginrequired && Account.loggedOut()) {
          postLogInRoute = $location.path();
          $location.path('/login').replace();
        } else if (postLogInRoute && Account.loggedIn()) {
    //once logged in,redirect to the last route and reset it
          $location.path(postLogInRoute).replace();
          postLogInRoute = null;
        }
    });
});
原文链接:https://www.f2er.com/angularjs/145007.html

猜你在找的Angularjs相关文章