Skip to content

Commit 0336d57

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 d968ec7 commit 0336d57

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
@@ -606,19 +606,20 @@ function $RouteProvider(){
606606
* @returns {string} interpolation of the redirect path with the parameters
607607
*/
608608
function interpolate(string, params) {
609-
var result = [];
610-
angular.forEach((string||'').split(':'), function(segment, i) {
611-
if (i === 0) {
612-
result.push(segment);
609+
return (string||'').replace(/(\/)?:(\w+)([?*])?/g, interpolateReplacer);
610+
611+
function interpolateReplacer(_, slash, key, option) {
612+
var optional = option === '?';
613+
var value = params[key];
614+
delete params[key];
615+
616+
if (optional && value == null) {
617+
return '';
613618
} else {
614-
var segmentMatch = segment.match(/(\w+)(.*)/);
615-
var key = segmentMatch[1];
616-
result.push(params[key]);
617-
result.push(segmentMatch[2] || '');
618-
delete params[key];
619+
slash = slash || '';
620+
return slash + (value || '');
619621
}
620-
});
621-
return result.join('');
622+
}
622623
}
623624
}];
624625
}

0 commit comments

Comments
 (0)