Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 img src is not found

I'm having a problem with sourcing an image with angular 4. It keeps telling me that the image is not found.

Folder structure:

app_folder/ app_component/   - my_componenet   - image_folder/       - myimage 

I am using the image in mycomponent. If in mycomponent, I insert it directly with:

<img src="img/myimage.png" 

This works fine, but I checked the html and it creates a hash. But my images have to be dynamically added to the html because they are part of a list. So, if I use:

<img src="{{imagePath}}"> 

which is basically img/myimage.png, it doesn't work. If I use:

<img [src]="imagePath"> 

It says NOT FOUND (htttps://localhost/img/myimage.png). Can you help me?

like image 806
Annk Avatar asked Jul 11 '17 17:07

Annk


People also ask

Why is my image not showing up in angular?

You're using the wrong path to the images. In the Angular project, you don't have to add the relative path from your file to image file. Angular resolves this problem for you, and in a component, you have to only add a path to the assets folder, instead of ../../assets.

How to bind img src in Angular?

To bind the src property of an <img> element to a component's property, place src in square brackets followed by an equal sign and then the property.

What is SRC in angular?

AngularJS ng-src Directive The ng-src directive makes sure the image is not displayed wrong before AngularJS has evaluated the code.

How do I add an image in angular 10?

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”> .


2 Answers

AngularJS

<img ng-src="{{imagePath}}"> 

Angular

<img [src]="imagePath"> 
like image 105
Yoav Schniederman Avatar answered Sep 30 '22 06:09

Yoav Schniederman


Copy your images into the assets folder. Angular can only access static files like images and config files from assets folder.

Try like this: <img src="assets/img/myimage.png">

like image 36
CharanRoot Avatar answered Sep 30 '22 06:09

CharanRoot