2

I need to set this properties on my div:

background: url();
background-repeat: no-repeat;
background-size: cover;
background-position: center, center;

This is my div:

<div *ngFor="let orderItems of receiptItems;" class="order-article animated bounceInLeft">
  <div class="order-img"   [style.background]="'url('+ orderItems.product.img.url +')'" ></div>
  <div class="order-title">
    <p>{{orderItems.product.title}}</p>
  </div>
</div>

As you can see guys I've set [style.background] url but I don't know how to set rest of the attributes / multiple attributes..

Thanks Any kind of help would be great !

-------- EDIT:

 <div *ngFor="let orderItems of receiptItems;" class="order-article animated bounceInLeft">
      <div class="order-img"   [ngStyle]="{'background':'url('+ orderItem.product.imgUrl +')', 'background-repeat':' no-repeat','background-size': 'cover;','background-position': 'center, center' }" ></div>
      <div class="order-title">
        <p>{{orderItems.product.title}}</p>
      </div>
    </div>

using ngStyle caused this:

enter image description here

attributes are not applied properly...

Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • Possible duplicate of [Angular2 dynamic background images](https://stackoverflow.com/questions/37575368/angular2-dynamic-background-images) – Prachi Jun 29 '18 at 11:28

2 Answers2

2

use the ngStyle tag

<div   [ngStyle]="{'background-image':'url( orderItem.product.imgUrl )', 'background-repeat':' no-repeat','background-size': 'cover','background-position': 'center, center' }"  class="order-img"  ></div>

Demo

Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
0

Try this.

CSS

.order-img{
background: url(--content);
background-repeat: no-repeat;
background-size: cover;
background-position: center, center;
}

HTML

<div *ngFor="let orderItems of receiptItems;" class="order-article animated bounceInLeft">
  <div class="order-img" [attr.outage-count]="orderItems.product.img.url" ></div>
  <div class="order-title">
    <p>{{orderItems.product.title}}</p>
  </div>
</div>
Abhishek Ekaanth
  • 2,511
  • 2
  • 19
  • 40