angular - 改变ThisBreaksApplicationSecurity angular2

标签 angular ngsanitize

我正在尝试在 angular2 中的标签中动态加载 pdf 文档,当我尝试更改 URL 时,抛出错误提示

SafeResourceUrlImplchangingThisBreaksApplicationSecurity: "localhost:8002/pdf.pdf"proto: SafeValueImplconstructor: SafeResourceUrlImpl()getTypeName: ()proto: Object

localhost:8002/pdf.pdf Failed to load resource: net::ERR_UNKNOWN_URL_SCHEME



这是我设置 URL 的方式,
当我需要显示组件时将调用此方法
public show(): void {
    this.visible = true;
    this.visibleAnimate = true;
    console.log(this.src)
    this.DocURL = this.sanitizer.bypassSecurityTrustResourceUrl(this.src);
    console.log(this.DocURL);
}


constructor(private sanitizer: DomSanitizer) {
    this.visible = false;
    this.visibleAnimate = false;

    this.DocURL = this.sanitizer.bypassSecurityTrustResourceUrl(window.location.host + "/pdf.pdf");
}

这是 HTML 部分
<div style=" height:650;width:870">
    <object width="870" height="650" type="application/pdf" [data]="DocURL" id="doc" #doc>
        <p>Not able to display the document</p>
    </object>
    <div style="display:none">
        <iframe id="fred" #fred style="border:1px solid #666CCC" title="PDF in an i-Frame" [src]="DocURL" frameborder="1" scrolling="auto" height="1100" width="850" ></iframe>
    </div>
</div>

我提供的 URL 将是动态的

最佳答案

您的 URL 没有协议(protocol)。请尝试以下操作:

this.DocURL = this.sanitizer.bypassSecurityTrustResourceUrl(window.location.protocol + '//' + window.location.host + "/pdf.pdf");

关于angular - 改变ThisBreaksApplicationSecurity angular2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41995986/

相关文章:

javascript - 在 JAVASCRIPT 中创建 UTC 日期对象

javascript - 如何将某物推送到返回数组的函数?(是否可以推送到返回数组的函数)

angular - 如何迭代可观察到的 firebase2 列表?

lowercase - AngularJS ngSanitize 说 "lowercase is not a function"

javascript - AngularJS:ngBindHtml 还包含子绑定(bind)?

angular - 无法读取未定义的 'xxx' 的属性

javascript - 如果我从数组中删除,如何发送 false?

javascript - 使用 Angular 转换 JSON 文件中的 HTML

javascript - 为什么 ngSanitize 依赖项会破坏我的应用程序?