R
R
Roman Rodionov2016-09-24 03:03:13
Angular
Roman Rodionov, 2016-09-24 03:03:13

How to wrap $routeProvider?

There is a standard code:

angular.module("main", ["ngRoute"])
        .config(RouterConfig);

    function RouterConfig($routeProvider){
        $routeProvider.when("/registration", {
            templateUrl: "/features/registration/view.html",
            controller: "RegistrationCtrl as reg"
        });
        //...
        $routeProvider.otherwise({redirectTo: '/registration'});
    }

Since there are a lot of $routeProvider.when() blocks, I want to make a service that will generate .config();
I'm trying to do it like this:
function Router(){
        this.features = [
            "registration",
            "timeLeft"
        ];
        this.bindProvider = function($routeProvider){
            for(var feature in this.features) {
                $routeProvider.when("/" + feature, {
                    templateUrl: "/features/" + feature + "/view.html",
                    controller: feature + "Ctrl as " + feature
                });
            }
        }
    }

The problem is that when you try to specify a service method in .config(), the service is not found.
\
angular.module("main", ["ngRoute"])
        .service("Router", Router)
        .config(Router.bindProvider); //Не видит сервис

How can I make sure that the config is generated automatically?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Novikov, 2016-09-24
@Romashishka

In general, you can’t reach $routeProvideroutside . config
Purely theoretically, you can try to use it decorator.
Another option is to reduce the number of routes with something like this:

$routeProvider.when('/features/:feature*', {
  templateUrl: function(urlParam){
    return '/pages/' + urlParam.name + '.html';
  },
  controller: 'someController'
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question