Skip to content

Commit 10e673c

Browse files
committed
fix(ngRoute): improve interpolation of params in string
When the redirect path contains optional or special parameters the interpolation function misses to remove them. This is also the case for the auto-generated redirection where a slash is appended or removed to a route. Change the way parameters are interpolated in a string. Remove undefined optional named groups and properly prepend a slash in case the named group was starting with one. Related to angular#5746.
1 parent c7a0b41 commit 10e673c

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/ngRoute/route.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -577,19 +577,18 @@ function $RouteProvider(){
577577
* @returns {string} interpolation of the redirect path with the parameters
578578
*/
579579
function interpolate(string, params) {
580-
var result = [];
581-
angular.forEach((string||'').split(':'), function(segment, i) {
582-
if (i === 0) {
583-
result.push(segment);
584-
} else {
585-
var segmentMatch = segment.match(/(\w+)(.*)/);
586-
var key = segmentMatch[1];
587-
result.push(params[key]);
588-
result.push(segmentMatch[2] || '');
580+
return (string||'').replace(/(\/)?:(\w+)([?*])?/g, function (_, slash, key, option) {
581+
var optional = option === '?';
582+
var value = params[key];
589583
delete params[key];
590-
}
584+
585+
slash = slash || '';
586+
587+
if (optional && value == null)
588+
return '';
589+
else
590+
return slash + (value || '');
591591
});
592-
return result.join('');
593592
}
594593
}];
595594
}

0 commit comments

Comments
 (0)