应用级功能

时间:2015-03-02 12:42:11

标签: angularjs resolve

我正在构建一个需要身份验证的应用程序,我需要在实例化路由控制器之前检查用户是否在更改路由时进行了身份验证,因此我需要一个函数来从服务器中检索已记录的用户,并且需要我需要的http调用'when'的resolve属性,然后将检索到的用户传递给控制器​​。我该如何声明该功能?

这是我的app.js

angular.module('MasterToolsApp', ['ui.bootstrap', 'ngRoute', 'ngSanitize', 'ngResource', 'ngAnimate', 'dialogs.main', 'toasty']);

angular.module('MasterToolsApp')
    .config(['$routeProvider', function($routeProvider) {

        $routeProvider
            .when('/', {
                redirectTo: '/login'
            })
            .when('/login', {
                templateUrl : 'dist/templates/login.html',
                controller  : 'LoginController',
                resolve     : ?
            })
            .when('/home', {
                templateUrl : 'dist/templates/home.html',
                controller  : 'HomeController',
                resolve     : ?
            })
            .when('/entries', {
                templateUrl : 'dist/templates/entries.html',
                controller  : 'EntriesController',
                resolve     : ?
            })
            .otherwise({ redirectTo: '/login' });

    }]);

1 个答案:

答案 0 :(得分:0)

我在另一个问题中找到了答案:Related Question

结果代码如下:

angular.module('MasterToolsApp', ['ui.bootstrap', 'ngRoute', 'ngSanitize', 'ngResource', 'ngAnimate', 'dialogs.main', 'toasty']);

var Helpers = {
    checkAuthentication: function($http) {

        return $http(
            {
                url     : '/auth',
                method  : 'GET',
                headers     : {'Content-Type': 'application/x-www-form-urlencoded'},
                timeout     : 10000
            }
        );

    }
};

angular.module('MasterToolsApp')
    .config(['$routeProvider', function($routeProvider) {

        $routeProvider
            .when('/', {
                redirectTo: '/login'
            })
            .when('/login', {
                templateUrl : 'dist/templates/login.html',
                controller  : 'LoginController',
                resolve     : {
                    user: ['$http', function($http) {
                            return Helpers.checkAuthentication($http);
                        }
                    ]
                }
            })
            .when('/home', {
                templateUrl : 'dist/templates/home.html',
                controller  : 'HomeController',
                resolve     : {
                    user: ['$http', function($http) {
                        return Helpers.checkAuthentication($http);
                    }
                    ]
                }
            })
            .when('/entries', {
                templateUrl : 'dist/templates/entries.html',
                controller  : 'EntriesController',
                resolve     : {
                    user: ['$http', function($http) {
                        return Helpers.checkAuthentication($http);
                    }
                    ]
                }
            })
            .otherwise({ redirectTo: '/login' });

    }]);