需求: 页面中有一个网页组件(由iframe编写),此iframe显示在一个输入框中,当修改输入框中地址的时候,要求改变网页组件中的内容
网页组件中的代码(html的部分)
<iframe
#Iframe
[src]="testUrl"
frameborder="0"
width="100%"
height="100%">
</iframe>
网页组件中的代码(ts的部分)
...省略
export class DesignWebInputComponent implements OnInit{
testUrl ;
}
此时问题出现了,页面无法显示内容
不要慌,有办法可以解决
constructor( private sanitizer:DomSanitizer) {}
导入DomSanitizer 这个类 并使用其中的bypassSecurityTrustResourceUrl() 转换url的格式 如下
trustUrl(url: string) {
if(url){
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
}
html中
<iframe
#Iframe
[src]="trustUrl(testUrl)"
frameborder="0"
width="100%"
height="100%">
</iframe>
在这里写了个trustUrl()转换 testUrl 这样就可以显示了
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。