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

feat(ngRoute):change pathRegExp to respect optional wildcard route params #14011

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ngRoute/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ function $RouteProvider() {

path = path
.replace(/([().])/g, '\\$1')
.replace(/(\/)?:(\w+)([\?\*])?/g, function(_, slash, key, option) {
var optional = option === '?' ? option : null;
var star = option === '*' ? option : null;
.replace(/(\/)?:(\w+)(\*\?|[\?\*])?/g, function(_, slash, key, option) {
var optional = (option === '?' || option === '*?') ? '?' : null;
var star = (option === '*' || option === '*?') ? '*' : null;
keys.push({ name: key, optional: !!optional });
slash = slash || '';
return ''
Expand Down
26 changes: 26 additions & 0 deletions test/ngRoute/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,32 @@ describe('$route', function() {
});


it('should properly process route params which are both eager and optional', function() {
module(function($routeProvider) {
$routeProvider.when('/foo/:param1*?/:param2', {templateUrl: 'foo.html'});
});

inject(function($location, $rootScope, $route) {
$location.path('/foo/bar1/bar2/bar3/baz');
$rootScope.$digest();

expect($location.path()).toEqual('/foo/bar1/bar2/bar3/baz');
expect($route.current.params.param1).toEqual('bar1/bar2/bar3');
expect($route.current.params.param2).toEqual('baz');
expect($route.current.templateUrl).toEqual('foo.html');

$location.path('/foo/baz');
$rootScope.$digest();

expect($location.path()).toEqual('/foo/baz');
expect($route.current.params.param1).toEqual(undefined);
expect($route.current.params.param2).toEqual('baz');
expect($route.current.templateUrl).toEqual('foo.html');

});
});


it('should properly interpolate optional and eager route vars ' +
'when redirecting from path with trailing slash', function() {
module(function($routeProvider) {
Expand Down