Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 30a9da5

Browse files
gonzaloruizdevillaIgorMinar
authored andcommitted
fix($route): correctly extract $routeParams from urls
Routes like '/bar/foovalue/barvalue' matching '/bar/:foo/:bar' now are well mapped in $routeParams to: {bar:'barvalue', foo:'foovalue'} Closes: #1501 Signed-off-by: Gonzalo Ruiz de Villa <gonzaloruizdevilla@gmail.com>
1 parent 25e1ad9 commit 30a9da5

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/ng/route.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,12 @@ function $RouteProvider(){
321321
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1") + '$',
322322
params = [],
323323
dst = {};
324-
forEach(when.split(/\W/), function(param) {
325-
if (param) {
326-
var paramRegExp = new RegExp(":" + param + "([\\W])");
324+
forEach(when.split(/[^\w:]/), function(param) {
325+
if (param && param.charAt(0) === ':') {
326+
var paramRegExp = new RegExp(param + "([\\W])");
327327
if (regex.match(paramRegExp)) {
328328
regex = regex.replace(paramRegExp, "([^\\/]*)$1");
329-
params.push(param);
329+
params.push(param.substr(1));
330330
}
331331
}
332332
});

test/ng/routeParamsSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,16 @@ describe('$routeParams', function() {
1717
expect($routeParams).toEqual({barId:'123', x:'abc'});
1818
});
1919
});
20+
21+
it('should correctly extract the params when a param name is part of the route', function() {
22+
module(function($routeProvider) {
23+
$routeProvider.when('/bar/:foo/:bar', {});
24+
});
25+
26+
inject(function($rootScope, $route, $location, $routeParams) {
27+
$location.path('/bar/foovalue/barvalue');
28+
$rootScope.$digest();
29+
expect($routeParams).toEqual({bar:'barvalue', foo:'foovalue'});
30+
});
31+
});
2032
});

0 commit comments

Comments
 (0)