Angular 动态背景图片

Angular dynamic background images

在 html 模板中,我使用动态图像的样式:

<div style="background: url('/img/{{item.img}}'); width: 200px; height: 150px"></div>

适用于网络浏览器和 android 浏览器。但是,使用 "style=" 的动态背景图像不会显示在 iPad 上。

我总是可以使用 img 标签创建动态图像,但我正在寻找 iPad 的 style/css 解决方案。

改用

<div [ngStyle]="{background: 'url(/img/' + item.img + ')', width: '200px', height: '150px'"></div>

<div [style.background]="'url(/img/' + item.img + ')'"
    [style.width.px]="200" [style.height.px]="150p"></div>

另见

你应该这样做,因为 +Günter Zöchbauer 第二部分答案至少在 Safari 上生成不需要的补充样式 background-position: initial 和 background-size: initial。

注意我只设置了背景图片样式:

<div [style.background-image]="'url(/img/' + item.img + ')'"
 [style.width.px]="200" [style.height.px]="150p"></div>
<div id="component-id" [style.background-image]="'url(www.domain.com/path/'+bgImageVariable+')'">
    <!-- ... -->
</div>

<div id="component-id" [style.background-image]="'url('+bgImageVariable+')'">
    <!-- ... -->
</div>

您可以通过在单个变量中添加 URL 路径以第二种方式使用。 例如

bgImageVariable="www.domain.com/path/img.jpg";

使用它并根据需要设置 div 标签的高度和宽度

<div id="component-id" [style.backgroundImage]="'url('+bgImageVariable+')'"></div>