【问题标题】:Trying to pass URL into Iframe Src Angular 2 getting an error尝试将 URL 传递到 Iframe Src Angular 2 得到错误
【发布时间】:2018-05-19 20:44:56
【问题描述】:

我正在尝试在点击事件中将 URL 动态添加到 iframe src,但出现此错误

Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'SafeValue%20must%20use%20%5Bproperty%5D'

我使用 domSanitizer 使 URL 可以安全地插入到 iframe

HTML

 <div class="cards-wrapper">
                <div class="prepackaged__card" *ngFor="let video of videos">

                    <img class="prepackaged__card-header" src="{{video.thumbnail}}">
                    <div class="prepackaged__card-body">
                        <div class="category">{{video.subname}}</div>
                        <h2 class="title">{{video.name}}
                        </h2>

                        <button (click)="sendUrl(video.videoData)">PLAY VIDEO</button>
                    </div>
                </div>
            </div>
<div class="video-player-modal">
     <div class="video-player-modal_header">

     </div>
     <div class="video-player-modal_video">
         <iframe class="video-player-modal_video_player" src="" frameborder="0" allowfullscreen=""></iframe>
     </div>
</div>

app.component.ts

import { Component, OnInit } from '@angular/core';
import { DashboardServiceProxy, UserVideoDto } from '@shared/service-proxies/service-proxies';
import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser';

declare var jQuery: any;
const $ = jQuery;

@Component({
  selector: 'app-video',
  templateUrl: './video.component.html',
  styleUrls: ['./video.component.scss'],
  providers: [
    DashboardServiceProxy
  ]
})
export class VideoComponent implements OnInit {
  videos: UserVideoDto[] = [];
  trustedDashboardUrl: SafeUrl;

  constructor(
    private _dashboardService: DashboardServiceProxy,
    private sanitizer: DomSanitizer
  ) {

    }

  ngOnInit() {
    this.getVideos();
  }

  getVideos() {
    this._dashboardService.getAllUserVideos().subscribe((result) => {
    this.videos = result;
    console.log(this.videos);
 });
}

  sendUrl(playerUrl) {
    this.trustedDashboardUrl = this.sanitizer.bypassSecurityTrustResourceUrl(playerUrl);
    $('.video-player-modal_video_player').attr('src', this.trustedDashboardUrl);
  }

}

对正在发生的事情有什么想法吗?

【问题讨论】:

  • 我在 HTML 中看不到 click 事件,该事件绑定在哪里以及组件中的处理程序在哪里?分享完整的代码。
  • 检查更新代码
  • playerUrl的格式是什么?如果它的 /your-route 则 iframe 源将其附加到您的应用程序 url,因此应用程序正在尝试使用未定义的路由在 iframe 内加载。

标签: javascript jquery angular typescript iframe


【解决方案1】:

我建议使用property binding 而不是使用jQuery 来动态填充属性。如下:

将 src 属性设置为初始化为空字符串的组件成员变量:

[src]="iframeURL"

在你的组件文件中设置 iframeURL:

iframeURL = '';

修改您的点击事件处理程序如下:

sendUrl(playerUrl) {
    // this.iframeURL = playerUrl // try it first, if it doesn't work use the sanitized URL
    this.trustedDashboardUrl = this.sanitizer.bypassSecurityTrustResourceUrl(playerUrl);
    this.iframeURL = this.trustedDashboardUrl;
}

希望它有效!如果没有,请分享。

【讨论】:

  • 是的,它有效,但是.. 我收到这些错误 Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'null'Content Security Policy: The page’s settings blocked the loading of a resource at data:application/javascript;base64,KGZ1b... (“script-src https://player.vimeo.com 'unsafe-inline' https://f.vimeocdn.com https://ssl.google-analytics.com https://js-agent.newrelic.com https://bam.nr-data.net https://f.vimeocdn.com”)
  • 这些看起来像是单独的问题:stackoverflow.com/questions/37693411/cannot-match-any-routes 表示第一个错误。
  • 第二个错误,检查:stackoverflow.com/questions/37298608/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2011-02-19
  • 1970-01-01
  • 2018-06-07
相关资源
最近更新 更多