【问题标题】:iframe not loading src in angular4 applicationiframe 未在 angular4 应用程序中加载 src
【发布时间】:2018-03-01 09:59:42
【问题描述】:

在我的应用程序中,我将 youtube 视频加载到 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>

下面是我运行相同的屏幕截图:

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

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

请指导。 谢谢。

【问题讨论】:

标签: angular iframe src


【解决方案1】:

你需要做一些这样的事情

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] = "" 的事情

【讨论】:

  • 我已经用 sanitiz 方法和错误更新了我的问题
  • 那么你只需做一些类似[src] = "url"的事情。你应该用整个方法更新问题,因为这是第一个失败点。
  • :你说的我都做了,但还是不行
  • 尝试使用嵌入网址而不是常规的https://www.youtube.com/embed/video-ID@ShrutiNair
  • 我也有同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 2019-03-07
  • 1970-01-01
  • 2017-01-23
相关资源
最近更新 更多