Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

displaying an embedded image in angular2

I'm working on a project in angular2, which involves loading an image from a database directly (base64 encoded). In angular1, one could simply load it as follows:

<img data-ng-src="data:image/jpg;base64,{{entry.img}}" />

where entry.img is the raw image data. However, I have no idea how to do this in angular2. I've tried

<img [src]="data:image/jpg;base64,{{entry.img}}" />

but that doesn't work, as angular still tries to load the image from a URL.

like image 580
veggiesaurus Avatar asked Mar 10 '16 10:03

veggiesaurus


People also ask

How to display a image in Angular?

Once you have put all the required images in the assets directory open the specific component typescript (. ts) file where you want to serve the image. Now you can add the URL of the image in a variable within the constructor so that it is initialized upon component creation.

How do you display images in angular components?

Upload the simple image you need to create an application using the command " ng new my-app". You have to open your HTML component and then use the image tag and put the image with a name or with their proper path, such as: <img src=” ../../assets/images/image. jpg” class=” img1”> .


1 Answers

Perhaps you could try this:

<img [src]="'data:image/jpg;base64,'+entry.img" />

assuming that entry is a property of the component and contains an img attribute.

like image 90
Thierry Templier Avatar answered Oct 14 '22 05:10

Thierry Templier