Skip to content

Commit 9fcdb38

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 a9c9f56 commit 9fcdb38

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/ngRoute/route.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -577,19 +577,20 @@ 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);
580+
return (string||'').replace(/(\/)?:(\w+)([?*])?/g, interpolateReplacer);
581+
582+
function interpolateReplacer(_, slash, key, option) {
583+
var optional = option === '?';
584+
var value = params[key];
585+
delete params[key];
586+
587+
if (optional && value == null) {
588+
return '';
584589
} else {
585-
var segmentMatch = segment.match(/(\w+)(.*)/);
586-
var key = segmentMatch[1];
587-
result.push(params[key]);
588-
result.push(segmentMatch[2] || '');
589-
delete params[key];
590+
slash = slash || '';
591+
return slash + (value || '');
590592
}
591-
});
592-
return result.join('');
593+
}
593594
}
594595
}];
595596
}

0 commit comments

Comments
 (0)