mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-09 23:34:42 +00:00
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>
This commit is contained in:
parent
741a37b338
commit
4c6b4447db
2 changed files with 16 additions and 4 deletions
|
|
@ -321,12 +321,12 @@ function $RouteProvider(){
|
||||||
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1") + '$',
|
var regex = '^' + when.replace(/([\.\\\(\)\^\$])/g, "\\$1") + '$',
|
||||||
params = [],
|
params = [],
|
||||||
dst = {};
|
dst = {};
|
||||||
forEach(when.split(/\W/), function(param) {
|
forEach(when.split(/[^\w:]/), function(param) {
|
||||||
if (param) {
|
if (param && param.charAt(0) === ':') {
|
||||||
var paramRegExp = new RegExp(":" + param + "([\\W])");
|
var paramRegExp = new RegExp(param + "([\\W])");
|
||||||
if (regex.match(paramRegExp)) {
|
if (regex.match(paramRegExp)) {
|
||||||
regex = regex.replace(paramRegExp, "([^\\/]*)$1");
|
regex = regex.replace(paramRegExp, "([^\\/]*)$1");
|
||||||
params.push(param);
|
params.push(param.substr(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -17,4 +17,16 @@ describe('$routeParams', function() {
|
||||||
expect($routeParams).toEqual({barId:'123', x:'abc'});
|
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'});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue