重构AngularJs路由配置
我有以下几个URL的路由配置,这些URL必须根据某个函数的值进行解析。下面是代码片段:重构AngularJs路由配置,angularjs,ngroute,resolve,Angularjs,Ngroute,Resolve,我有以下几个URL的路由配置,这些URL必须根据某个函数的值进行解析。下面是代码片段: $routeProvider .when('/url_with_restrictions', { templateUrl: '/pages/private/add_entity/add_gasstation.html', controller: 'ControllerName', // r
$routeProvider
.when('/url_with_restrictions', {
templateUrl: '/pages/private/add_entity/add_gasstation.html',
controller: 'ControllerName',
// restrict angularjs routing
resolve: {
validate: functionToBeInjected(){
var validateAccess = $q.defer();
return validateAccess.promise;
}
}
})
如何定义函数
functionToBeInjected()
并在控制器的每个resolve
中使用它?您可以提取一个函数来设置路由
function addRoute(url, template, controller) {
functionToBeInjected = ...
$routeProvider
.when(url, {
templateUrl: template,
controller: controller,
// restrict angularjs routing
resolve: {
validate: functionToBeInjected(){
var validateAccess = $q.defer();
return validateAccess.promise;
}
}
})
};