Route Specificity related to Declaration Order

54 views
Skip to first unread message

Matt Hughes

unread,
Feb 28, 2014, 12:55:48 PM2/28/14
to ang...@googlegroups.com

Given the following routes:

when("/") ...
when("/foo") ...
when("/:bar") ...
when("/baz") ...

I would think a $location of "/baz" would hit the last route, but it doesn't.  It instead matches on the :bar route.  If I switch the declaration order:

when("/baz") ...
when("/:bar") ...

it works as expected.  Shouldn't the route matcher consider constants in the path more specific than catch all route params? 





Sander Elias

unread,
Mar 1, 2014, 12:41:18 AM3/1/14
to ang...@googlegroups.com
Hi Matt,

Short answer, No.
Somewhat longer, the /:bar is a 'catch all' construct. How can it know that you don't want to handle 'baz' as an variable?
In this case it is very obvious, but if your router grows, or is build more dynamically  that might not be the case.  

Regards
Sander


Matt Hughes

unread,
Mar 3, 2014, 1:46:42 PM3/3/14
to ang...@googlegroups.com
Well, the router could take that into account, no?  If it has two routes that match and one has a literal and the other has a matcher, the literal should have precedence.

Luke Kende

unread,
Mar 4, 2014, 1:48:36 AM3/4/14
to ang...@googlegroups.com
Nope.  That's not how it works... you must specify the routes in order of literals first as Sander mentioned, and, as  you found when trying it out.  Precedence is given on first rule that matches, not on looking through all the rules and guessing what the user wants.  

Matt Hughes

unread,
Mar 4, 2014, 9:31:09 AM3/4/14
to ang...@googlegroups.com
Right.  I'm proposing a change to the algorithm.  I'll see what I come up with and see if anyone is interested in a PR.


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/iJO1LqjbsXI/unsubscribe.
To unsubscribe from this group and all its topics, 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/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages