5

I am trying to simply display a base64 image in an Ionic app.

The image won't display if I do this:

HTML:

 <img ng-src="data:image/jpeg;base64,{{myImage}}"/>

Controller:

$scope.myImage= "/9j/4AAQSkZJ ...";

But the image WILL display if I just put the encoded string directly in the image element like this:

 <img ng-src="data:image/jpeg;base64,/9j/4AAQSkZJ ..."/>

I have checked every unsafe security setting, looked at dozens of other SO posts, etc. If I put this small example in a CodePen, it works both ways.

What could be happening to the $scope.myImage variable that would prevent it from binding to the image element? Is it an ionic thing? An angular issue?

1
  • Here's what the HTML looks like when served: <img ng-src="data:image/jpeg;base64," src="data:image/jpeg;base64,"> Commented Jan 16, 2015 at 14:39

4 Answers 4

8

Use data-ng-src directive like this <img data-ng-src="{{data.image_url}}">.

In your controller set base64 string as this: $scope.data.image_url=<your base64 image source>

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

i had the same issue, solved it by configuring the main angular module with whitelisting this way:

angular.module('app').config(function ($compileProvider) {
$compileProvider.imgSrcSanitizationWhitelist(/^\s*(https|ftp|mailto|file|tel|data)/);})

Comments

-2

Sometimes I too face the issue, and use setTimeout or $timeout.

setTimeout(function(){
   $scope.myImage= "/9j/4AAQSkZJ ...";
}, 2000);

In Angular way (add $timeout DI in controller)-

$timeout(function(){
   $scope.myImage= "/9j/4AAQSkZJ ...";
}, 2000);

3 Comments

Interesting thought, but the timer had no effect.
@lilbiscuit Can you please paste your base64 encoded data ?
I have determined that there's a conflict somewhere in my app. If I start with a blank project I don't have this problem.
-3

just use ng-if at the div, and then the img tag gets rendered as soon as the data is loaded

Comments

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.