AngularJS: Routing with URL having optional parameters

1,457 views
Skip to first unread message

Shyam

unread,
Apr 12, 2014, 3:01:29 AM4/12/14
to ang...@googlegroups.com
I have a URL in my app.js containing routes. lets say the url is: /api/:opt1/:opt2/:opt3/users

I want that my url will work in any case, i should able to ignore the optional parameters( opt1, opt2 or opt3, may be all or few). How can I achieve this.

is there any way that i can call $location.path('/users'), and i can specify also what are the values of the optional param ?

Please suggest.

Erik Starck

unread,
Apr 12, 2014, 3:30:35 AM4/12/14
to ang...@googlegroups.com
Perhaps not the prettiest solution but this should work if I understand your problem correctly.

$routeProvider.
        when('/api/users'...).
        when('/api/:opt1/users'...).
        when('/api/:opt1/:opt2/users'...).
        when('/api/:opt1/:opt2/:opt3/users'...)

In 1.2 named groups (wildcards) are supported. Read the docs here:

From the docs:
"For example, routes like /color/:color/largecode/:largecode*\/edit will match /color/brown/largecode/code/with/slashes/edit and extract:
color: brown
largecode: code/with/slashes."


BR Erik



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Shyam

unread,
Apr 12, 2014, 11:17:09 PM4/12/14
to ang...@googlegroups.com
Thanks Erik, this is really helpful.
Reply all
Reply to author
Forward
0 new messages