Angularjs 应用程序级功能

Angularjs 应用程序级功能,angularjs,resolve,Angularjs,Resolve,我正在构建一个需要身份验证的应用程序,在实例化路由控制器之前,我需要检查用户在更改路由时是否经过身份验证,因此,我需要一个函数来使用$http从服务器检索登录用户,我需要在'when'的resolve属性中调用该函数,然后将检索到的用户传递给控制器。如何声明函数 这是我的app.js angular.module('MasterToolsApp', ['ui.bootstrap', 'ngRoute', 'ngSanitize', 'ngResource', 'ngAnimate', 'dial

我正在构建一个需要身份验证的应用程序,在实例化路由控制器之前,我需要检查用户在更改路由时是否经过身份验证,因此,我需要一个函数来使用$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' });

    }]);

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

生成的代码如下所示:

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

    }]);