Angular 4 img src is not found

后端 未结 16 1497
我寻月下人不归
我寻月下人不归 2020-12-24 10:11

I\'m having a problem with sourcing an image with angular 4. It keeps telling me that the image is not found.

Folder structure:

app_folder/
app_compo         


        
相关标签:
16条回答
  • 2020-12-24 10:41

    AngularJS

    <img ng-src="{{imagePath}}">
    

    Angular

    <img [src]="imagePath">
    
    0 讨论(0)
  • 2020-12-24 10:42

    Well, the problem was that, somehow, the path was not recognized when was inserted in src by a variable. I had to create a variable like this:

    path:any = require("../../img/myImage.png");
    

    and then I can use it in src. Thanks everyone!

    0 讨论(0)
  • 2020-12-24 10:43

    Angular can only access static files like images and config files from assets folder. Nice way to download string path of remote stored image and load it to template.

      public concateInnerHTML(path: string): string {
         if(path){
            let front = "<img class='d-block' src='";
            let back = "' alt='slide'>";
            let result = front + path + back;
            return result;
         }
         return null;
      }
    

    In a Component imlement DoCheck interface and past in it formula for database. Data base query is only a sample.

    ngDoCheck(): void {
       this.concatedPathName = this.concateInnerHTML(database.query('src'));
    }
    

    And in html tamplate <div [innerHtml]="concatedPathName"></div>

    0 讨论(0)
  • 2020-12-24 10:44

    You have to mention the width for the image as default

     <img width="300" src="assets/company_logo.png">
    

    its working for me based on all other alternate way.

    0 讨论(0)
  • 2020-12-24 10:47

    in your component assign a variable like,

    export class AppComponent {
      netImage:any = "../assets/network.jpg";
      title = 'app';
    }
    

    use this netImage in your src to get the image, as given below,

    <figure class="figure">
      <img [src]="netImage" class="figure-img img-fluid rounded" alt="A generic square placeholder image with rounded corners in a figure.">
      <figcaption class="figure-caption">A caption for the above image.</figcaption>
    </figure>
    
    0 讨论(0)
  • 2020-12-24 10:47

    @Annk you can make a variable in the __component.ts file

    myImage : string = "http://example.com/path/image.png";

    and inside the __.component.html file you can use one of those 3 methods :

    1 .

    <div> <img src="{{myImage}}"> </div>
    

    2 .

    <div> <img [src]="myImage"/> </div>
    

    3 .

    <div> <img bind-src="myImage"/> </div>
    
    0 讨论(0)
提交回复
热议问题