1

I am using AngularJS and Cordova, and I'm using Visual Studios 2013 to simulate the cordova application.

I have a bunch of image files inside the ms-appdata:///local/ directory and I'm trying to show those images by using ng-src="{{img}}" where $scope.img = "ms-appdata:///local/image.jpg"

HTML

 <img ng-src="{{img}}" alt="" />

JavaScript

 $scope.img= "ms-appdata:///local/image.jpg";

When I run this code I dont even get the missing image icon thing at the top, its almost like there is no image at all.

when I change ng-src="{{img}}" to src="ms-appdata:///local/image.jpg" the image shows up no problem.

I have already tried $sce.trustAsResourceUrl("ms-appdata:///local/image.jpg").$$unwrapTrustedValue() as another Stack Overflow answer suggested doing.

1 Answer 1

1

$sce.trustAsResourceUrl can help to interpolate url on src attribute directly, but when ng-src directive assigns interpolated value to src attribute, it is already a string, not trusted object. The information if this url was trusted is lost in ng-src.

$sce.trustAsResourceUrl("...").$$unwrapTrustedValue() doesn't make sense in this context, because it will give you the same "..." untrusted string on the output.

I believe the best thing that can be done in this case is to add a new protocol to the white list.

app.config(function ($compileProvider) {
  var whitelist = $compileProvider.imgSrcSanitizationWhitelist();
  whitelist = new RegExp(whitelist.source.replace(/(ftp)/, '$1|ms-appdata'));
  $compileProvider.imgSrcSanitizationWhitelist(whitelist);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Are you trying to replace ms-appdata to ftp protocol in the code?
@Gandhi It replaces ftp with ftp|ms-appdata in the middle of ^\s*((https?|ftp|file|blob):|data:image\/) regexp (which may vary between Angular versions).
Specifying the following lines in the config explicitly is not working too - "$compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|ftp|file|content|blob‌​|ms-appx|ms-appdata|x-wmapp0|unsafe|local):|data:image\//);" Any ideas?
@Gandhi Nope. Does it work in the opposite direction, when you remove https?| from whitelisted protocols? I would suggest to open a new question that replicates your problem and contains your own code.
Hi, thanks for the info. Was trying to help out someone on this issue. Thes issue was not with sanitization but with compileprovider getting overriden somewhere else losing all the changes. Fixed now it seems. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.