Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Angular serve images outside assets folder

Tags:

angular

Am building a project on Angular and this project will have lots of images as part the design. Am wondering if it is possible to keep images used by each component within the component instead of having to fetch each image from the assets folder.

I want to know if it is possible to configure Angular to house and serve images used by a component within that component.

See picture for clarification:

Sample App

like image 449
Alex Imenwo Avatar asked Nov 22 '18 13:11

Alex Imenwo


People also ask

Where should I put images 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.

Do images go in assets folder?

Keep an assets folder that contains top-level CSS, images, and font files. In react best practices we keep an assets folder inside the src which may contain top-level CSS, images, and font files.

What is the use of assets folder in angular?

The Angular application has a default assets folder. By default, whatever you put in the assets folder you can access it directly from the browser and queried through an HTTP request. You can create your own folder like assets by adding that folder to the assets section in the angular. json file.

What is the Public folder in angular?

The Angular-cli places all files in the /dist directory. The public folder is for public assets and the content is copied to the /dist folder.


1 Answers

Assets Contains image files and other asset files to be copied as-is when you build your application.

But Yes, you can!

But for this you need to manually require each image in your component file and use dyanmically in template like this -

declare var require: any
myImage = require('./myImage.png');

<img [src]="myImage" />

As by default Angular CLI has a configuration for static data such as Images/Fonts/Css files etc from assets folder.

But if you want to use some static data outside from there you need to require like above mentioned.

PS: Not sure whether angular takes all static data at the time of build such as image at the component level. But should work in ng serve

like image 62
Pardeep Jain Avatar answered Sep 28 '22 02:09

Pardeep Jain