Skip to content

Commit a7816d1

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 6bdb6d3 commit a7816d1

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
@@ -638,7 +638,7 @@ function $RouteProvider() {
638638
if (i === 0) {
639639
result.push(segment);
640640
} else {
641-
var segmentMatch = segment.match(/(\w+)(.*)/);
641+
var segmentMatch = segment.match(/(\w+)(?:[?*])?(.*)/);
642642
var key = segmentMatch[1];
643643
result.push(params[key]);
644644
result.push(segmentMatch[2] || '');

test/ngRoute/routeSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,29 @@ describe('$route', function() {
919919
});
920920

921921

922+
it('should properly interpolate optional and eager route vars ' +
923+
'when redirecting from path with trailing slash', function() {
924+
module(function($routeProvider) {
925+
$routeProvider.when('/foo/:id?/:subid?', {templateUrl: 'foo.html'});
926+
$routeProvider.when('/bar/:id*/:subid', {templateUrl: 'bar.html'});
927+
});
928+
929+
inject(function($location, $rootScope, $route) {
930+
$location.path('/foo/id1/subid2/');
931+
$rootScope.$digest();
932+
933+
expect($location.path()).toEqual('/foo/id1/subid2');
934+
expect($route.current.templateUrl).toEqual('foo.html');
935+
936+
$location.path('/bar/id1/extra/subid2/');
937+
$rootScope.$digest();
938+
939+
expect($location.path()).toEqual('/bar/id1/extra/subid2');
940+
expect($route.current.templateUrl).toEqual('bar.html');
941+
});
942+
});
943+
944+
922945
it('should allow custom redirectTo function to be used', function() {
923946
function customRedirectFn(routePathParams, path, search) {
924947
expect(routePathParams).toEqual({id: 'id3'});

0 commit comments

Comments
 (0)