2

I'm writing a simple product information management app using angular js. To keep my app as modular as possible i've split it into multiple modules with one module "pim" as startpoint. For each module I want to have a different route, so that it is easy to plug in a new module or remove it without having to maintain a huge route in the pim module config.

Currently I have two routes (the first route):

(function(){
angular 
    .module("pim")
    .config(router)

function router($routeProvider){
    $routeProvider
        .when("/",{
            templateUrl: "view/info.html",
            controller: "pimController"
        })
        .when("/info",{
            templateUrl: "view/info.html",
            controller: "pimController"
        })
        .when("/alcohol",{
            templateUrl: "view/alcohol.list.html",
            controller: "alcoholController"
        });
    }
})();

The second route

(function(){
angular 
    .module("alcohol")
    .config(router)

function router($routeProvider){
    $routeProvider
        .when("/alcohol/list",{
            templateUrl: "view/alcohol.list.html",
            controller: "alcoholController"
        })
        .when("/alcohol/info",{
            templateUrl: "view/alcohol.info.html",
            controller: "alcoholController"
        });
    }
})();

As you can see /alcohol has a templateUrl and a controller, the same as /alcohol/list, but i want to know if there is a simple (standard) way to change to another URL for example /alcohol/list, so that I do not have to repeat the templateUrl and controller and keep this information in the alcohol module, where it belongs.

For example

.when("/alcohol",{
    routeTo: "/alcohol/list"
})

Thank you for your help

SOLVED

The option to redirect exists, did not look in the $routeProvider documentation well enough:

.when("/alcohol",{
     redirectTo:"/alcohol/list"
});

The code above works

1 Answer 1

4

You can use $routeProvider's redirectTo route map.

.when("/alcohol", {
  redirectTo: "/alcohol/list"
});

Read more: https://docs.angularjs.org/api/ngRoute/provider/$routeProvider

Sign up to request clarification or add additional context in comments.

2 Comments

I would say you solved it, but I'm not sure if you read my solution first and then copied it. So I'll say it's usefull
Nope, I actually browsed the docs and found it :) It's just a coincidence that we found it at the same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.