http://plnkr.co/edit/cFeD3tLOYWcPVwMAFQG3?p=preview
How to replace all occurrences of a string in JavaScript?
You can use interpolate service of angularjs. Interpolate service works with angular js expressions, which should consist {{ }} instead of {} for replacement.
app.controller('MainCtrl', function($scope,$interpolate) {
$scope.name = 'World';
function escapeRegExp(string) {
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
function replaceAll(string, find, replace) {
return string.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
$scope.interpolate = function ( ) {
var context = { cars :"myCars", color:"red"};
var url = "/json/feed?car=${cars}&colour=${color}&model=VW";
var t1 = replaceAll(url,"{","{{");
var url2 = replaceAll(t1,"}","}}");
var exp = $interpolate(url2);
console.log(exp(context));
}
$scope.interpolate();
});
Output : /json/feed?car=$myCars&colour=$red&model=VW