Skip to content

Commit 07c046c

Browse files
committed
Merge pull request #1585 from just-boris/master
fix($urlRouter): resolve clashing of routes
2 parents 5910337 + b5c57c8 commit 07c046c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/urlRouter.js

+6
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
305305
return listener;
306306
}
307307

308+
rules.sort(function(ruleA, ruleB) {
309+
var aLength = ruleA.prefix ? ruleA.prefix.length : 0;
310+
var bLength = ruleB.prefix ? ruleB.prefix.length : 0;
311+
return bLength - aLength;
312+
});
313+
308314
if (!interceptDeferred) listen();
309315

310316
return {

test/urlRouterSpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ describe("UrlRouter", function () {
5555
return path.replace('baz', 'b4z');
5656
}).when('/foo/:param', function($match) {
5757
match = ['/foo/:param', $match];
58+
}).when('/foo/bar', function($match) {
59+
match = ['/foo/bar', $match];
5860
}).when('/bar', function($match) {
5961
match = ['/bar', $match];
6062
});
@@ -71,6 +73,18 @@ describe("UrlRouter", function () {
7173
});
7274
});
7375

76+
it("should handle more specified url first", function() {
77+
location.path("/foo/bar");
78+
scope.$emit("$locationChangeSuccess");
79+
expect(match[0]).toBe("/foo/bar");
80+
expect(match[1]).toEqual({});
81+
82+
location.path("/foo/baz");
83+
scope.$emit("$locationChangeSuccess");
84+
expect(match[0]).toBe("/foo/:param");
85+
expect(match[1]).toEqual({param: 'baz'});
86+
});
87+
7488
it("should execute rewrite rules", function () {
7589
location.path("/foo");
7690
scope.$emit("$locationChangeSuccess");

0 commit comments

Comments
 (0)