这是对我有用的简化版本:
原文链接:https://www.f2er.com/angularjs/145007.htmlapp = 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; } }); });