Gün*_*uer 68
请改用
<div [ngStyle]="{background: 'url(/img/' + item.img + ')', width: '200px', height: '150px'"></div>
Run Code Online (Sandbox Code Playgroud)
要么
<div [style.background]="'url(/img/' + item.img + ')'"
[style.width.px]="200" [style.height.px]="150p"></div>
Run Code Online (Sandbox Code Playgroud)
您应该这样做,因为+GünterZöchbauer第二部分答案会产生不需要的补充风格背景位置:初始和背景大小:至少在Safari上初始化.
请注意,我只设置了背景图像样式:
<div [style.background-image]="'url(/img/' + item.img + ')'"
[style.width.px]="200" [style.height.px]="150p"></div>
Run Code Online (Sandbox Code Playgroud)
小智 7
<div id="component-id" [style.background-image]="'url(www.domain.com/path/'+bgImageVariable+')'">
<!-- ... -->
</div>
Run Code Online (Sandbox Code Playgroud)
或者
<div id="component-id" [style.background-image]="'url('+bgImageVariable+')'">
<!-- ... -->
</div>
Run Code Online (Sandbox Code Playgroud)
您可以通过在单个变量中添加 URL 路径以第二种方式使用。例如
bgImageVariable="www.domain.com/path/img.jpg";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39985 次 |
| 最近记录: |