AngularJS使用resolve验证多个路由

时间:2013-07-06 15:05:42

标签: javascript php angularjs model-view-controller url-routing

我正在尝试为我的应用程序中的大多数路由验证用户。

有没有办法在所有路线上全球完成?所以我不需要以下内容:

resolve : {
    //This function is injected with the AuthService where you'll put your authentication logic
    'auth' : function(AuthService){
        return AuthService.authenticate();
    }
}

每次$routeProvider.when()来电。

1 个答案:

答案 0 :(得分:2)

Gloopy的建议非常有趣,我将来可能会采用类似的方法。

现在我采取了一种更简单的方法:

gm.config(['$routeProvider', 'PathProvider', function($routeProvider, PathProvider) {

    var authResolver = { // although this does work there could be a better way to do this.
        'auth' : function(AuthenticationService) {
            return AuthenticationService.isLoggedIn();
        }
    };

    $routeProvider.when('/authenticatedRoute', { 
        templateUrl: PathProvider.view('application/dashboard/index.html'), 
        controller: 'dashboardController',
        resolve: authResolver
    }); 

    $routeProvider.otherwise({ 
        redirectTo: '/dashboard',
        resolve: authResolver
    });

}]);