1

My app structure is like:

index.html
main.ts
app
 |--main.component.html
 |--main.component.ts
 |--app.module.ts
 |--note.png
... etc

I want to use the note.png in main.component.html which is under the same folder. I tried <img src="note.png"/> and <img src="./note.png"/>, but the picture is not shown. The browser goes to /note.png to search for the picture.

Is there any good way to locate the resource using relative path?
I cannot use because I may have many pages under different folders, their base href are different.

9
  • Angular is different from the normal html structure. In normal html, if we visit main.component.html, the url will be like /app/main.component.html. So if we use relative path in the html, it will goes to /app/note.png. But in Angular, the url is /index.html, but actually the page of main.component.html will be shown. Is there any way to solve this problem? Commented Nov 3, 2017 at 6:02
  • you should ng-src Commented Nov 3, 2017 at 6:06
  • Let me know does my answer solve your problem or not? Commented Nov 3, 2017 at 6:09
  • Thanks very much for replying. Is there ng-src in Angular? I am not using AngularJS. Even if I may use ng-src, what about those in style, like: style="background-image: url(note.png); " Commented Nov 3, 2017 at 6:10
  • 1
    I believe you need to save it in an "Assets" folder at the root level of your app. eg: "your-application\src\assets\note.png" Commented Nov 3, 2017 at 6:18

1 Answer 1

3

If you are using the Angular CLI, you can put your assets in any folders that you want. You just need to then add those folders to your .angular-cli.json file here:

  "assets": [
    "assets",
    "favicon.ico"
  ],

So if you wanted some of your images in an img folder, you'd modify it like this:

  "assets": [
    "assets",
    "img",
    "favicon.ico"
  ],

Hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, but I want to put my images in the same folder with ts and html who use the image. I don't want to create a asserts folder to store all the pictures of all the pages
I believe you can configure webpack to get assets from anywhere, but it's horrible. Just use assets and organize it correctly. 1 folder per component for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.