diff --git a/src/urlRouter.js b/src/urlRouter.js index 05ef8dc81..d1f102e6d 100644 --- a/src/urlRouter.js +++ b/src/urlRouter.js @@ -303,6 +303,12 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) { return listener; } + rules.sort(function(ruleA, ruleB) { + var aLength = ruleA.prefix ? ruleA.prefix.length : 0; + var bLength = ruleB.prefix ? ruleB.prefix.length : 0; + return bLength - aLength; + }); + if (!interceptDeferred) listen(); return { diff --git a/test/urlRouterSpec.js b/test/urlRouterSpec.js index ab916c051..2acebb407 100644 --- a/test/urlRouterSpec.js +++ b/test/urlRouterSpec.js @@ -55,6 +55,8 @@ describe("UrlRouter", function () { return path.replace('baz', 'b4z'); }).when('/foo/:param', function($match) { match = ['/foo/:param', $match]; + }).when('/foo/bar', function($match) { + match = ['/foo/bar', $match]; }).when('/bar', function($match) { match = ['/bar', $match]; }); @@ -71,6 +73,18 @@ describe("UrlRouter", function () { }); }); + it("should handle more specified url first", function() { + location.path("/foo/bar"); + scope.$emit("$locationChangeSuccess"); + expect(match[0]).toBe("/foo/bar"); + expect(match[1]).toEqual({}); + + location.path("/foo/baz"); + scope.$emit("$locationChangeSuccess"); + expect(match[0]).toBe("/foo/:param"); + expect(match[1]).toEqual({param: 'baz'}); + }); + it("should execute rewrite rules", function () { location.path("/foo"); scope.$emit("$locationChangeSuccess");