From 9fcdb381f453fc4a27c42f657b1093b832235892 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Tue, 15 Jul 2014 12:46:49 +0200 Subject: [PATCH] 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 #5746. --- src/ngRoute/route.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/ngRoute/route.js b/src/ngRoute/route.js index 1f992b695bfa..62756b804702 100644 --- a/src/ngRoute/route.js +++ b/src/ngRoute/route.js @@ -577,19 +577,20 @@ function $RouteProvider(){ * @returns {string} interpolation of the redirect path with the parameters */ function interpolate(string, params) { - var result = []; - angular.forEach((string||'').split(':'), function(segment, i) { - if (i === 0) { - result.push(segment); + return (string||'').replace(/(\/)?:(\w+)([?*])?/g, interpolateReplacer); + + function interpolateReplacer(_, slash, key, option) { + var optional = option === '?'; + var value = params[key]; + delete params[key]; + + if (optional && value == null) { + return ''; } else { - var segmentMatch = segment.match(/(\w+)(.*)/); - var key = segmentMatch[1]; - result.push(params[key]); - result.push(segmentMatch[2] || ''); - delete params[key]; + slash = slash || ''; + return slash + (value || ''); } - }); - return result.join(''); + } } }]; }