应用级功能
App Level Function
我正在构建一个需要身份验证的应用程序,我需要在实例化路由控制器之前检查更改路由时用户是否经过身份验证,因此我需要一个函数来使用 $http 从服务器检索登录的用户我需要调用 'when' 的解析 属性,然后将检索到的用户传递给控制器。如何声明函数?
这是我的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' });
}]);
我在另一个问题中找到了答案: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' });
}]);
我正在构建一个需要身份验证的应用程序,我需要在实例化路由控制器之前检查更改路由时用户是否经过身份验证,因此我需要一个函数来使用 $http 从服务器检索登录的用户我需要调用 'when' 的解析 属性,然后将检索到的用户传递给控制器。如何声明函数?
这是我的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' });
}]);
我在另一个问题中找到了答案: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' });
}]);