简体   繁体   English

Angular 2 - 动态设置图像URL

[英]Angular 2 - Dynamically set image URL

The image path is defined in item.imagePath. 图像路径在item.imagePath中定义。

<div *ngFor="let item of itemList">
   <div style="background-image: url(item.imagePath)" class="thumb">
       <span>{{item.productName}}</span>
   </div>
   <div>
       <p>{{item.productFeature}}</p>
   </div>
</div>

style="background-image: url(item.imagePath)"
What is the correct syntax as this does not work? 什么是正确的语法,因为这不起作用?

The styles are updated according to the value of the expression evaluation: 样式根据表达式评估的值更新:

keys are style names with an optional . 键是具有可选项的样式名称。 suffix (ie 'top.px', 'font-style.em'), values are the values assigned to those properties (expressed in the given unit). 后缀(即'top.px','font-style.em'),值是分配给这些属性的值(以给定单位表示)。

<div [ngStyle]="{'background-image': 'url(' + photo + ')'}"></div>

Try this 试试这个

 <div *ngFor="let item of itemList">
       <div class="thumb">
           <img src="{{item.imagePath}}" />
           <span>{{item.productName}}</span>
       </div>
       <div>
           <p>{{item.productFeature}}</p>
       </div>
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM