我有以下html:
<li>
<div class="w3l_banner_nav_right_banner1" style="background:url('./assets/images/2.jpg') no-repeat 0px 0px;">
<h3>Make your <span>food</span> with Spicy.</h3>
<div class="more">
<a href="products.html" class="button--saqui button--round-l button--text-thick" data-text="Shop now">Shop now</a>
</div>
</div>
</li>
Run Code Online (Sandbox Code Playgroud)
问题:
我想/assets/images/2.jpg用动态变量替换图像url {{ article.uri }}.
我尝试了几种方式从下面ref:
到目前为止尝试过:
<li *ngFor="let article of arr;let i=index;">
<div *ngIf="i == 0" class="w3l_banner_nav_right_banner" [ngStyle]="{ 'background-url': 'url('+article.uri+')'} no-repeat 0px 0px;">
<h3>Make your <span>food</span> with Spicy.</h3>
<div class="more">
<a href="products.html" class="button--saqui button--round-l button--text-thick" data-text="Shop now">Shop now1</a>
</div>
</div>
</li>
Run Code Online (Sandbox Code Playgroud)
我正在使用Angular 4.1.3.
Fre*_*din 13
background-url是不正确的CSS,使用background或background-image替代.
以下是正确语法的示例:
<div [ngStyle]="{'background': '#fff url(' + article.uri + ') no-repeat 0 0'}"></div>
Run Code Online (Sandbox Code Playgroud)
您的完整示例如下所示:
<li *ngFor="let article of arr; let i=index;" >
<div *ngIf="i == 0"
class="w3l_banner_nav_right_banner"
[ngStyle]="{'background': '#fff url(' + article.uri + ') no-repeat 0 0'}" >
<h3> Make your <span>food</span> with Spicy. </h3>
<div class="more">
<a href="products.html" class="button--saqui button--round-l button--text-thick" data-text="Shop now">Shop now1</a>
</div>
</div>
</li>
Run Code Online (Sandbox Code Playgroud)
如果您从远程源或用户输入获取背景图像,则需要对角度进行角度清理.在您的组件中,您将需要添加以下代码...
import { DomSanitizer } from '@angular/platform-browser';
export class YourComponent {
constructor(private _sanitizer: DomSanitizer) { }
public sanitizeImage(image: string) {
return this._sanitizer.bypassSecurityTrustStyle(`url(${image})`);
}
}
Run Code Online (Sandbox Code Playgroud)
尝试将HTML设置为此...
<li *ngFor="let article of arr;let i=index;">
<div *ngIf="i == 0" class="w3l_banner_nav_right_banner" [style.background-image]="sanitizeImage(article.uri)">
<h3>Make your <span>food</span> with Spicy.</h3>
<div class="more">
<a href="products.html" class="button--saqui button--round-l button--text-thick" data-text="Shop now">Shop now1</a>
</div>
</div>
</li>
Run Code Online (Sandbox Code Playgroud)
并no-repeat 0px 0px;在你附加到div的某些类中应用.
| 归档时间: |
|
| 查看次数: |
18820 次 |
| 最近记录: |