DemanB
DemanB

Reputation: 47

Angular $route change url based on route

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

Upvotes: 2

Views: 332

Answers (1)

Andras Szell
Andras Szell

Reputation: 527

You can use $routeProvider's redirectTo route map.

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

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

Upvotes: 4

Related Questions