Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path to Images not Working in Angular 2

Everything I have put on my site functions correctly, but every image that I try to put into the site (with the exception of the background) gives back a 404 Not Found Error.

All of my images are located at "Project/src/assets/images" and I have tried the following img tags in my html documents. The documents are located in "Project/src/app/tabs".

<img src="src/assets/images/image_name.jpg"> 
<img src="/src/assets/images/image_name.jpg">
<img src="./src/assets/images/image_name.jpg">

I set the background image for my page using css and it works. This is the selector and the attribute I have applied:

background: url('assets/images/greybackground.jpg') no-repeat center center fixed;

How can I add images to my HTML documents without getting the 404 error?

like image 857
Logan Kitchen Avatar asked Feb 20 '17 05:02

Logan Kitchen


People also ask

Why images are not loading 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 integrate image in Angular?

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

How to display image file in Angular?

To show the image preview in Angular, we declared the img HTML tag and bind the src tag to the variable. We will assign the image URL to the src variable using the new FileReader() method.


1 Answers

Your html files are in "Project/src/app/tabs" and you are trying to access from that folder thats why images are not loading. So you need to come back from tabs folder and app folder to reach the base directory for both your code and images. Use ../ to come back from a folder. So after reaching base directory just use normal directory listings.

So answer is

<img src="../../assets/images/image_name.jpg">
<img src="../../assets/images/image_name.jpg">
<img src="../../assets/images/image_name.jpg">
like image 106
Jijo Cleetus Avatar answered Oct 19 '22 20:10

Jijo Cleetus