如何在.html模板Angular中的div上定义多种样式

时间:2018-06-29 11:17:06

标签: javascript html css angular css-selectors

我需要在div上设置此属性:

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

这是我的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>

如您所见,我已经设置了[style.background] url,但我不知道如何设置其余属性/多个属性。

谢谢 任何帮助都会很棒!

--------编辑:

 <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>

使用ngStyle导致此:

enter image description here

属性未正确应用...

2 个答案:

答案 0 :(得分:1)

使用ngStyle标记

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

Demo

答案 1 :(得分:0)

尝试一下。

  

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>