Cybermate
Cybermate

Reputation: 105

AngularJS Route : set several parameters values for the same route

I was wondering if it was possible with AngularJS to do something like this :

I currently have a route with a first parameter called 'resource' that can be "device" or "group". There is a second parameter called 'id' but it doesn't matter. With the following code the route accepts anything as first parameter :

.when("/templates/:resource/:id", {
            templateUrl: "/templates/views/navigation/templates.html",
            controller: 'ctrlTemplates',
            tab: "templates",
        })

It makes me check in the controller if the value of the first parameter is either 'device' or 'group'. I would like to get rid of this verification part without creating two routes if possible :

.when("/templates/group/:id", {
                templateUrl: "/templates/views/navigation/templates.html",
                controller: 'ctrlTemplates',
                tab: "templates",
            })
.when("/templates/device/:id", {
                templateUrl: "/templates/views/navigation/templates.html",
                controller: 'ctrlTemplates',
                tab: "templates",
            })

So my question is, is it possible, using ONE route, to have several urls ? Something like this for instance :

.when("/templates/('device'|'group')/:id", {
            templateUrl: "/templates/views/navigation/templates.html",
            controller: 'ctrlTemplates',
            tab: "templates",
        })

So I don't have to check the value of the parameter afterwards in the controller myself using

if([('group', 'device'].includes($routeParams.resource))...

Do you know if it's possible ? Or a similar way to do it?

Best regards,

Upvotes: 0

Views: 38

Answers (2)

Cybermate
Cybermate

Reputation: 105

I am willing to switch to ui-router if it can help me achieve what I want to do. Also, if it supports types it's nice as I have parameters that must be integers. I am going to have a look at some examples.

If you have any example with regexes It would be nice !

Upvotes: 0

Nix
Nix

Reputation: 58572

You can't do what you want. If you want to use ui-router you can pull it off but AngularJS ngRoute does not give you the ability to use regexes, or bind route parameters to types. If you are willing to switch to ui-router I can give you an example of how to do what you want.

Upvotes: 1

Related Questions