Skip to content

Commit bbfea05

Browse files
committed
fix($route): fix redirection with optional/eager params
Previously, when (automatically) redirecting from path that fetured a trailing slash and optional or "eager" parameters, the resulting path would (incorrectly) contain the special characters (`?`,`*`) along with the parameter values. Closes angular#9819
1 parent 33ca191 commit bbfea05

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/ngRoute/route.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ function $RouteProvider() {
634634
if (i === 0) {
635635
result.push(segment);
636636
} else {
637-
var segmentMatch = segment.match(/(\w+)(.*)/);
637+
var segmentMatch = segment.match(/(\w+)(?:[?*])?(.*)/);
638638
var key = segmentMatch[1];
639639
result.push(params[key]);
640640
result.push(segmentMatch[2] || '');

test/ngRoute/routeSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,29 @@ describe('$route', function() {
904904
});
905905

906906

907+
it('should properly interpolate optional and eager route vars ' +
908+
'when redirecting from path with trailing slash', function() {
909+
module(function($routeProvider) {
910+
$routeProvider.when('/foo/:id?/:subid?', {templateUrl: 'foo.html'});
911+
$routeProvider.when('/bar/:id*/:subid', {templateUrl: 'bar.html'});
912+
});
913+
914+
inject(function($location, $rootScope, $route) {
915+
$location.path('/foo/id1/subid2/');
916+
$rootScope.$digest();
917+
918+
expect($location.path()).toEqual('/foo/id1/subid2');
919+
expect($route.current.templateUrl).toEqual('foo.html');
920+
921+
$location.path('/bar/id1/extra/subid2/');
922+
$rootScope.$digest();
923+
924+
expect($location.path()).toEqual('/bar/id1/extra/subid2');
925+
expect($route.current.templateUrl).toEqual('bar.html');
926+
});
927+
});
928+
929+
907930
it('should allow custom redirectTo function to be used', function() {
908931
function customRedirectFn(routePathParams, path, search) {
909932
expect(routePathParams).toEqual({id: 'id3'});

0 commit comments

Comments
 (0)