Eugenia Kim Eugenia Kim - 4 months ago 14
AngularJS Question

Angular JS route parameters with limited options

I just started using Angular and currently using 1.4.2.
I would like to know how to have only three options for the URL,

/:type
- create, update, and view.

They all use the same controller, so I don't want to have separate whens such as

.when('/create ... , .when('/update ... , .when('/view ... .


I don't want any other options to be passed beside these.
It would be great if I could get some help!
Thanks :)

Answer

you could just get the parameter from $routeParams service on your controller and do whatever logic you want to do with it. see https://docs.angularjs.org/api/ngRoute/service/$routeParams

route config

$routeProvider
    .when("/yourroute/:type", {
        template: "default"
        templateUrl: "template.html",
        controller: "Controller"
    });

controller function

function Controller($routeParams) {
    switch($routeParams.type) {
        case "create":
            ...
        case "update":
            ...
        case "view":
            ...
        default:
            ...
    }
}