[英]Using a SafeUrl in a background image CSS expression with ngStyle?
嘗試像這樣使用SafeUrl
設置背景圖像。
<h1>Image Test</h1>
<img [src]="imageURL" width="500px" height="500px">
<br>
<code>{{imageURL}}</code>
<div id="i"
class=".background-image"
[ngStyle]="{'background-image': imageURL}">
></div>
img
標簽呈現,但div
不呈現。
有任何想法嗎?
這是 Stackblitz 演示:
https://stackblitz.com/edit/angular-blob-to-safe-url?file=src%2Fapp%2Fapp.component.html
圖像是這樣加載的:
this.loadImage().subscribe(i=>
{
this.image = i
this.imageURL = this.sanitizer.bypassSecurityTrustUrl(URL.createObjectURL(this.image))
})
URL 的背景圖像屬性語法是 url("[URL]")
html
<div id="i"
class=".background-image"
[ngStyle]="{'background-image': getBackgroundImageUrl(imageURL)}">
></div>
ts
getBackgroundImageUrl(url) {
return `url("${url.changingThisBreaksApplicationSecurity}")`;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.