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

Commit

Permalink
fix($route): correctly extract $routeParams from urls
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
gonzaloruizdevilla authored and Gonzalo Ruiz de Villa committed Dec 11, 2012
1 parent a66c968 commit 773ac4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/ng/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,12 @@ function $RouteProvider(){
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1") + '$',
params = [],
dst = {};
forEach(when.split(/\W/), function(param) {
if (param) {
var paramRegExp = new RegExp(":" + param + "([\\W])");
forEach(when.split(/[^\w:]/), function(param) {
if (param && param.charAt(0) === ':') {
var paramRegExp = new RegExp(param + "([\\W])");
if (regex.match(paramRegExp)) {
regex = regex.replace(paramRegExp, "([^\\/]*)$1");
params.push(param);
params.push(param.substr(1));
}
}
});
Expand Down
12 changes: 12 additions & 0 deletions test/ng/routeParamsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ describe('$routeParams', function() {
expect($routeParams).toEqual({barId:'123', x:'abc'});
});
});

it('should correctly extract the params when a param name is part of the route', function() {
module(function($routeProvider) {
$routeProvider.when('/bar/:foo/:bar', {});
});

inject(function($rootScope, $route, $location, $routeParams) {
$location.path('/bar/foovalue/barvalue');
$rootScope.$digest();
expect($routeParams).toEqual({bar:'barvalue', foo:'foovalue'});
});
});
});

0 comments on commit 773ac4a

Please sign in to comment.