AngularJS如何在登录后回重定向到下一个路线 [英] AngularJS How to redirect back to the next route after logging in

查看:127
本文介绍了AngularJS如何在登录后回重定向到下一个路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是('$ routeChangeStart',函数(事件,接下来,电流)使用$ rootScope $,我重定向到登录页面如果路由需要身份验证,这完美的作品。

Using the $rootScope.$on('$routeChangeStart', function(event, next, current), I'm redirecting to the signin page if the route requires authentication. That works perfectly.

我如何重定向回到预定航线虽然在登录后?

How do I redirect back to the intended route though after signing in?

推荐答案

这是什么为我工作的一个简化版本:

This is a simplified version of what's working for me:

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'})
});

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

Then in the application's run block:

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;
        }
    });
});

这篇关于AngularJS如何在登录后回重定向到下一个路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆