Angular 4 img src is not found

后端 未结 16 1576
我寻月下人不归
我寻月下人不归 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条回答
  •  旧时难觅i
    2020-12-24 10:58

    An important observation on how Angular 2, 2+ attribute bindings work.

    The issue with [src]="imagePath" not working while the following do:

    Is due your binding declaration, [src]="imagePath" is directly binded to Component's this.imagePath or if it's part of an ngFor loop, then *each.imagePath.

    However, on the other two working options, you're either binding a string on HTML or allowing HTML to be binded to a variable that's yet to be defined.

    HTML will not throw any error if you bind , however Angular will.

    My recommendation is to use an inline-if in case the variable might not be defined, such as , which can be though of as node.src = imagePath ? imagePath : 'something'.

    Avoid binding to possible missing variables or make good use of *ngIf in that element.

提交回复
热议问题