Angular 2,DomSanitizer,bypassSecurityTrustHtml,SVG

Angular 2,DomSanitizer,bypassSecurityTrustHtml,SVG,angular,angular-dom-sanitizer,Angular,Angular Dom Sanitizer,我一直在使用带有html字符串中SVG的Domsanizer 在当前版本的Angular之前,此功能运行良好: this.domsanizer.bypassSecurityTrustHtml(内容) 现在我得到了一个叫做 SafeHtmlImpl {changingThisBreaksApplicationSecurity: "<svg> blah </svg>"} changingThisBreaksApplicationSecurity SafeHtmlImpl{更

我一直在使用带有html字符串中SVG的Domsanizer

在当前版本的Angular之前,此功能运行良好:

this.domsanizer.bypassSecurityTrustHtml(内容)

现在我得到了一个叫做

SafeHtmlImpl {changingThisBreaksApplicationSecurity: "<svg> blah </svg>"}
changingThisBreaksApplicationSecurity
SafeHtmlImpl{更改此中断应用程序的安全性:“废话”}
更改此突破性应用程序安全性
现在有没有一种新的方法来访问DomSanitizer的输出?我应该以安全HTML类型或其他方式接收它吗?如果仍然过滤html,那么绕过SecurityTrustHTML有什么意义


明信片上有答案吗?请…

使用
DOMSanizer.bypassSecurityTrustHtml

constructor(private sanitizer: DomSanitizer) {
}

let html = this.sanitizer.bypassSecurityTrustHtml("<svg> blah </svg>");
构造函数(专用消毒剂:domsanizer){
}
设html=this.sanitizer.bypassSecurityTrustHtml(“blah”);
更多信息:

演示:

从“@angular/platform browser”导入{domsanizer}
@管道({name:'safeHtml'})
导出类SafeHtmlPipe实现PipeTransform{
构造函数(私有消毒:domsanizizer){}
转换(值){
log(this.sanitized.bypassSecurityTrustHtml(值))
返回此.sanitized.bypassSecurityTrustHtml(值);
}
}
@组成部分({
选择器:“我的应用程序”,
模板:`
`,
})
导出类应用程序{
名称:字符串;
html:safeHtml;
构造函数(){
this.name='Angular2'
this.html=“blah”;
}
}

这也可以通过对象括号表示法完成:

let safeHtml = this.domSanitizer.bypassSecurityTrustHtml(content);
console.log(safeHtml["changingThisBreaksApplicationSecurity");

必须这样做是因为
safeHtml.Changing此Breaksapplicationsecurity
对我不起作用。

那么有什么问题吗?我已将您的svg更改为其他svg以使其起作用。谢谢。关于管道的好主意,我已经像那样安装好了,而且工作正常。实际上没有做任何功能上不同的事情,但谁在抱怨呢?这很有效。谁在抱怨?这意味着我没有在抱怨!感谢您的帮助因为SVG可能会造成安全威胁,强烈建议您事先对SVG进行清理。使用这是OP在他的问题中使用的。这在我当前的版本中对我有效。我的问题不是OP所说的,而是我得到了我想要的。许多解决方案建议使用@Pipe并添加domsanizer safeHTML,但这正是我最后想要尝试的。通过您的解决方案,我能够在组件文件中使用DOMSanizer。谢谢你,安德烈。