I have a variable
x = "http://example.com?a={{a}}&b={{b}}
This variable is then used in a
ng-src={{x}}
Therefore it is important for me to url encode the variables a and b.
What i do currently is:
var func = $interpolate($scope.x);
var url = func($scope);
return $sce.trustAsResourceUrl(url);
My problem is that when a or b contains spaces they are not url encoded.
How can i tell the $interpolate function to url encode the variables a and b?
As you want to see the url econded, you are not doing encoding of parameters anywhere. You need to encode your
aas well asbin{{}}interpolation usingencodeURIComponentof javascript. For that you need to create a wrapper method in scope that will callencodeURIComponentmethod and return the encodeURL, method would be like below.Thereafter your
URLwould look likehttp://www.example.com/images.jpg?a={{encodeContent(a)}}&b={{encodeContent(b)}}And while attaching it to
srcofimgtag you need to evaluate interpolation first & then you can make that url as trusted as you are doing now.Markup
Code
Working Plunkr