重构AngularJs路由配置

时间:2015-10-30 16:25:58

标签: angularjs ngroute resolve

我有以下路由配置,其中包含几个必须根据某些功能的值解析的URL。这里是代码片段:

 $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中使用它?

1 个答案:

答案 0 :(得分:0)

您只需提取一个功能来设置路线

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