更新:我建议人们首先查看上面的链接,因为有一些好的解决方案。
但是,由于我的问题与网址无关,而与宽度和高度有关,因此我无法让它们中的任何一个适用于我的特殊情况。
问题:
我在控制台中收到带有以下代码的以下警告:
WARNING: sanitizing unsafe style value calc(300px * 0.7509025270758123) (see http://g.co/ng/security#xss).
该代码可以正常工作,但可以为页面上的每个图像运行该代码,并填满控制台。
任何想法如何消除警告?
<div
*ngIf="swipeFile.imageUrl"
class="linear-background"
[ngStyle]="{'width': '300px', 'height': 'calc(300px * ' + (swipeFile.imageHeight / swipeFile.imageWidth) + ')'}" >
<img mat-card-image [src]="swipeFile?.imageUrl" alt="image">
</div>
答案 0 :(得分:1)
请尝试这个
<div
*ngIf="swipeFile.imageUrl"
class="linear-background"
[ngStyle]="{'width': '300px', 'height': (swipeFile.imageHeight / swipeFile.imageWidth) * 300 + 'px'}" >
<img mat-card-image [src]="swipeFile?.imageUrl" alt="image">
</div>
答案 1 :(得分:1)
这也可以,但是@Amir的答案更好。
<div *ngIf="swipeFile.imageUrl" class="linear-background" [style]="getSanitizedStyle('width: 300px; height: ' + (300 * (swipeFile.imageHeight / swipeFile.imageWidth)).toFixed(0) + 'px')" >
.ts
constructor(private sanitizer: DomSanitizer) { }
public getSanitizedStyle(style: string) {
return this.sanitizer.bypassSecurityTrustStyle(style);
}