Skip to content

Commit

Permalink
fix(ngRoute): improve interpolation of params in string
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
avdv committed Aug 4, 2014
1 parent a9c9f56 commit 9fcdb38
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/ngRoute/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
}
}
}];
}

0 comments on commit 9fcdb38

Please sign in to comment.