typescript – Angular2 – 警告:消除不安全的风格值url(SafeVa
发布时间:2020-12-17 07:48:10 所属栏目:安全 来源:网络整理
导读:我想在我的Angular 2应用程序的组件模板中设置DIV的背景图像.但是,我在控制台中收到以下警告,我没有得到所需的效果…我不确定动态CSS背景图像是否被阻止,因为Angular2中的安全限制,或者我的HTML模板是坏的. 这是我在控制台中看到的警告(我已将我的img url更
我想在我的Angular 2应用程序的组件模板中设置DIV的背景图像.但是,我在控制台中收到以下警告,我没有得到所需的效果…我不确定动态CSS背景图像是否被阻止,因为Angular2中的安全限制,或者我的HTML模板是坏的.
这是我在控制台中看到的警告(我已将我的img url更改为/img/path/is/correct.png: WARNING: sanitizing unsafe style value url(SafeValue must use [property]=binding: /img/path/is/correct.png (see http://g.co/ng/security#xss)) (see http://g.co/ng/security#xss). 事情是我使用Angular2中的DomSanitizationService来清理注入到我的模板中的内容.这是我在我的模板中的HTML: <div> <div> <div class="header" *ngIf="image" [style.background-image]="'url(' + image + ')'"> </div> <div class="zone"> <div> <div> <h1 [innerHTML]="header"></h1> </div> <div class="zone__content"> <p *ngFor="let contentSegment of content" [innerHTML]="contentSegment"></p> </div> </div> </div> </div> </div> 这是组件… Import { DomSanitizationService,SafeHtml,SafeUrl,SafeStyle } from '@angular/platform-browser'; @Component({ selector: 'example',templateUrl: 'src/content/example.component.html' }) export class CardComponent implements OnChanges { public header:SafeHtml; public content:SafeHtml[]; public image:SafeStyle; public isActive:boolean; public isExtended:boolean; constructor(private sanitization:DomSanitizationService) { } ngOnChanges():void { map(this.element,this); function map(element:Card,instance:CardComponent):void { if (element) { instance.header = instance.sanitization.bypassSecurityTrustHtml(element.header); instance.content = _.map(instance.element.content,(input:string):SafeHtml => { return instance.sanitization.bypassSecurityTrustHtml(input); }); if (element.image) { /* Here is the problem... I have also used bypassSecurityTrustUrl */ instance.image = instance.sanitization.bypassSecurityTrustStyle(element.image); } else { instance.image = null; } } } } } 请注意,当我刚刚使用[src] =“image”绑定到模板时,例如: <div *ngIf="image"> <img [src]="image"> </div> 并且图像被传递使用bypassSecurityTrustUrl一切似乎工作正常…任何人都可以看到我做错了什么?
您必须将整个url语句包装在bypassSecurityTrustStyle中:
<div class="header" *ngIf="image" [style.background-image]="image"> </div> 还有 if (element.image) { instance.image = instance.sanitization.bypassSecurityTrustStyle('url(' + element.image + ')'); } else { instance.image = null; } 否则不会被视为有效的样式属性 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- shell – 如何在zip压缩文件中列出文件而不解压缩
- vs2005 combox(winform) 绑定数据时触发Selected
- Scala的“if … else”可以作为库函数实现吗?
- 指令和控制器
- Docker - 解决 Error response from daemon: dri
- 跟我一起学Redis之Redis持久化必知必会
- twitter-bootstrap – Codemirror内容直到被单击
- twitter-bootstrap – 如何使用Yii Framework中的
- angular2 – * ngIf和* ngFor在相同的元素导致错
- Casbah Scala MongoDB驱动程序 – 嵌入式对象
热点阅读