我正在使用 ngFor 输出一组帖子。每个都应该有一个背景图像。 getBackgroundStyle 从帖子中提取图像的 URL(这是一个数组)
<div class="singlePost" *ngFor="let post of data" (click)="itemTapped($event, post)">
<div style="background-image: url('{{getBackgroundStyle(post)}}')">
<img src="{{getBackgroundStyle(post)}}">
现在,有趣的是,在构建之后,img 标签工作正常,而 div 完全失去了 style 标签
实验室/浏览器中的输出:
<div class="singlePost">
<div>
<img src="http://theurl.com">
最佳答案
改用属性绑定(bind):
<div [ngStyle]="{ background-image: 'url(' + getBackgroundStyle(post) + ')' }"></div>
或
<div [style.background-image]="'url(' + getBackgroundStyle(post) + ')'"></div>
图像元素也应该这样做:
<img [src]="getBackgroundStyle(post)">
关于javascript - 来自变量的 Ionic3 背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44618067/