2015-03-02 64 views
1

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

    }]); 

回答

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

    }]);