Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline style background-image using ngFor

Tags:

angular

I am trying to set the background image for each div in an ngFor loop. In the JSON I'm looping over I have a property property.Image that holds a url string to an image. <img src={{flag.properties.Image}}/> works, but my attempt to show the image as a background-image does not. In the browser Inspector the div background-image shows as {{flag.properties.Image}} rather than reading the url that I'm trying to point to. What am I doing wrong?

<h3>Top Flags</h3>
<div class="grid grid-pad">
  <div *ngFor="let flag of flags" (click)="gotoDetail(flag)" class="col-1-4">
    <div class="module flag" style="background-image: url({{flag.properties.Image}})">
      <h4>{{flag.id}}</h4>
      <div><img src={{flag.properties.Image}} height="50px" /></div>
      <div>{{flag.properties.Adaptation}}</div>
    </div>
  </div>
</div>
like image 574
jm22 Avatar asked Jul 16 '26 03:07

jm22


1 Answers

Use instead

[style.background-image]="'url('+flag.properties.Image+')'"

See also In RC.1 some styles can't be added using binding syntax

like image 71
Günter Zöchbauer Avatar answered Jul 17 '26 18:07

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!