Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images not loading in Angular 2

Tags:

html

css

angular

HTML File :

 <div>
      <img  src="New-Google-Logo.png"/>
    </div>

Here the New-Google-Logo.png is in the same folder as in the html file. But after ng serve the html page loads with other details, but not the image. Tried by directly giving a link for an image (like www.google.com/images/x.png), it works, but local file is not loading.

Folder Tree :

src
  -app
    -logincomponent
              - logincomponent.html
              - logincomponent.css
              - New-Google-Logo.png
              - logincomponent.ts
     -homecomponent
              - homecomponent.html
              - homecomponent.css
              - homecomponent.ts

Here the New-Google.png is referred inside logincomponent.html as given above.

Try 2 :

src
  -app
    -logincomponent
              - logincomponent.html
              - logincomponent.css
              - Images
                - New-Google-Logo.png
              - logincomponent.ts

And referred in the html like :

<div>
      <img  src="./images/New-Google-Logo.png"/>
 </div>

Both these didn't worked out.

like image 693
Amal Avatar asked Aug 28 '17 13:08

Amal


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 show images 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.

How to insert image in Angular?

You can insert an image file or online image in the document using the insertImage() method. Refer to the following sample code. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { enableProdMode } from '@angular/core'; import { AppModule } from './app.


2 Answers

If you are using angular-cli then all your static assets should be kept in assets folder. Then you should give path as

 <div>
      <img  src="assets/images/New-Google-Logo.png"/>
 </div>

When you serve the project all static assets needs to be served to client in order to display it on client. Angular cli build and bundle entire project in js files. To serve your static assets you can follow two ways

  • put all your static assets in assets folder which angular-cli serves with default .angular-cli.json
  • or you need to make entry of the static assets folder in .angular-cli.json file in array named as assets as my images folder is in static folder and static folder is at same hierarchy level of assets folder

    "assets": [ "assets", "static/images" ]
    
like image 63
Prathmesh Dali Avatar answered Sep 22 '22 06:09

Prathmesh Dali


change path to

<img  src="assets/images/New-Google-Logo.png"/>

and add height and width

 <img  src="assets/images/New-Google-Logo.png" width="200" height="100">
like image 44
Codelover Avatar answered Sep 22 '22 06:09

Codelover