2

In root of project src there is directory assets where are placed images.

In user service I read these images like:

return (data.photo == undefined || data.photo == null) ? '../../../assets/img/blank-person.jpg' :

You can see related path, that is bad approach. How to specify absolute path to assets?

POV
  • 11,293
  • 34
  • 107
  • 201

2 Answers2

6

@OPV you don't need to provide complete path, as we know Angular ultimately build the project even we perform ng serve. So, folder structure becomes like below image :

folder structure of Distribution Build

here you can see asset folder is parallel to index.html file which ultimately using your image. Hence you need to provide path considering above structure.

for my test.jpg file

path will be : './assets/img/test.jpg'

For example : <div style="background-image: url('./assets/img/test.jpg')" ></div>

Here ./ indicates current folder, as above structure shows that index file is in current folder of asset folder we have to start path from current folder.

Hope it will resolve your query, if you find any problem please let me know I will be happy to help.

sajal rajabhoj
  • 507
  • 4
  • 14
2

To get assets folder from any components inside the src folder,

You have to modified your code like:

return (data.photo == undefined || data.photo == null) ? './assets/img/blank-person.jpg' :
Trusha
  • 611
  • 5
  • 12