0

This is the profile structure of my angular6 app:

enter image description here

Client.component.html is supposed to display the image "defaultProfilePic.png".

So it contains the following line of code:

<img [src]="url"  width="220" height="200">

Client.component.ts contains the following line of code:

private url = "../asset/images/defaultProfilePic.png";

For some reason this doesn't work. Does anybody have any idea why that might be?

Tommy
  • 699
  • 4
  • 11
  • 26

3 Answers3

5

Yeah, the problem is you have created your own custom directory and you are trying to serve images from it.
you should use assets folder to store your static assets, images etc.

if you want to store images and static assets in another directory you need to tell CLI about it by making an entry in assets array of the angular.json file and you need to ensure that it is on the same level as the assets folder. i.e create a custom folder under app

"assets": [
              "src/favicon.ico",
              "src/assets"
               src/asset
            ],

Another mistake from your side is private url =

All databound properties must be typescript public property.

why? Refer this


and you can provide absolute path instead of the relative path because CLI knows about it

public url = "asset/images/defaultProfilePic.png";
Vikas
  • 11,859
  • 7
  • 45
  • 69
  • ok ... I created a new folder "assets" (location: "src/assets") ... in it I created an "images" folder, so the images folder is src/assets/images. I then put the following line into the file Client.component.ts: public url = "assets/images/defaultProfilePic.png"; ... however, it still doesn't work – Tommy Jun 11 '18 at 19:05
  • ... and of course, the image file is inside the images folder (forgot to mention that) – Tommy Jun 11 '18 at 19:13
  • it works now ... thank you very much ... just had to restart the app – Tommy Jun 11 '18 at 19:16
1

That sound like your path isn't the correct one try by adding another ../ and make sure that you have the following setting in .angular-cli.json

"assets": [
  "assets",
  "favicon.ico"
],
Nikita Popov
  • 896
  • 10
  • 19
Abel Valdez
  • 2,368
  • 1
  • 16
  • 33
1

I found a way to show Angular static assets (.png in my case) in Stackblitz.

  1. Upload your project in github
  2. Go to asset/Image
  3. Open your image in github
  4. Open it in new tab
  5. Copy link
  6. Replace the link within your github html code project
  7. Open project in Stackblitz
  8. Done