Skip to content

Commit b5c57c8

Browse files
committed
fix($urlRouter): resolve clashing of routes
1 parent 1f781c8 commit b5c57c8

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
@@ -303,6 +303,12 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
303303
return listener;
304304
}
305305

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

308314
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)