iframe没有在angular4应用程序中加载src

时间:2017-09-20 11:06:04

标签: angular iframe src

在我的应用程序中,我将youtube vedio加载到iframe中。 我已经清理了网址。但是当我运行应用程序时,src属性不会加载。

下面是html:

<a [href]="data.url" target="_blank" *ngIf="data.contentTypeString=='YOUTUBE_VIDEO' && data.url != null">
            <div class="embed-responsive embed-responsive-16by9">
                <iframe *ngIf="data.contentTypeString=='YOUTUBE_VIDEO'" 
                        class="embed-responsive-item" 
                        src="{{data.url}}"></iframe>
            </div>
        </a>
下面是我运行相同时的屏幕截图: enter image description here

src不会加载 我试过[src] = data.url。

以下是我用来清理网址的方法:

public manageYoutubeUrl(url: any) {
        console.log("url====" + url);
        var youtubeUrl = this.sanitize.bypassSecurityTrustUrl(url.replace('watch?v=', 'embed/').replace('http://', 'https://')+"?wmode=opaque&rel=0&autohide=1&showinfo=0&wmode=transparent");
        console.log("youtubeUrl=====" + youtubeUrl);
        return youtubeUrl;
    }

当我检查youtubeURL的日志时,它会像这样:

  

youtubeUrl ===== SafeValue必须使用[property] = binding:   https://www.youtube.com/embed/l-103taCWOQ?wmode=opaque&rel=0&autohide=1&showinfo=0&wmode=transparent   (见http://g.co/ng/security#xss

     

错误错误:需要一个安全的ResourceURL,得到一个URL(参见   http://g.co/ng/security#xss)       在DomSanitizerImpl.checkNotSafeValue(

请指导。 感谢。

1 个答案:

答案 0 :(得分:2)

你需要做这样的事情

<强> TS

 import { DomSanitizer } from '@angular/platform-browser';

 this.current_url=this.sanitizer.bypassSecurityTrustResourceUrl(url)

<强>模板

<iframe id="frame" frameborder="0" [src]="current_url"></iframe>

评论后更新

只需执行[src]="current_url"[attr.src] = ""

等操作即可
相关问题