【发布时间】:2018-02-25 13:51:26
【问题描述】:
我正在制作一个电影网站,现在正在制作一个带有预告片的部分。我从 youtube api 抓取了 3 个视频文档,并且已经显示了它们的缩略图并将它们的 id 属性设置为 youtube.com 上视频的 id。
现在,当我单击缩略图标题时,我想打开一个模式窗口,其中显示我选择的电影的 youtube 预告片。我通过抓取视频 ID 并将其动态放置在下面的 iframe 元素中来做到这一点(嵌入 youtube 视频需要此元素)。不幸的是,这似乎不起作用。
<iframe width="560" height="315" src="{{link of youtube video gets inserted here after I click on one of the trailer buttons}}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
它向我显示的是一个空模式和以下错误。有谁知道如何解决这个问题?
错误:资源 URL 上下文中使用的不安全值
这就是模态框的样子:它显示的是一个空的模态框,而不是我点击的链接的 youtube 视频。
HTML 文件:这里我将组件中的 embedLink (url) 变量插入我需要嵌入 Youtube 视频的 iframe 元素中
<div class="col-md-8 col-md-offset-2">
<h5>Welcome to MovieMeter! <span *ngIf="isLoggedIn"> You are logged in as {{fullName}}</span></h5>
<br>
<h3>Trailers:</h3>
<hr>
<ul>
<li *ngFor="let trailer of trailers">
<img src="{{trailer.body.items[0].snippet.thumbnails.high.url}}" alt="nope">
<div id="{{trailer.body.items[0].id.videoId}}" #trailerTitle (click)="openModal(modal, trailerTitle.id)" class="trailerTitle"><h5>{{trailer.movie.title}}</h5></div>
</li>
</ul>
<app-cinema-featured></app-cinema-featured>
</div>
<!-- The Modal -->
<div #modal id="myModal" class="modal">
<!-- Modal content -->
<div #modalContent class="modal-content">
<span (click)="closeModal(modal)" class="close">×</span>
<iframe width="560" height="315" src="{{embedLink}}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>
</div>
组件:在 openModal() 方法中,我创建了动态 youtube URL,然后将其分配给 embedlink 变量
export class HomeComponent implements OnInit {
trailers;
embedLink = null;
constructor(private modalService:NgbModal, private authService: AuthService, private movieService: MovieService){}
ngOnInit(){
// get the thumbnails and links of the three most recent movie trailers via the youtube API
this.movieService.getTrailers()
.subscribe(trailers => {
this.trailers = trailers.result;
console.log(this.trailers);
})
}
openModal(modal, id){
this.embedLink = 'https://www.youtube.com/embed/' + id;
modal.style.display = "block";
}
closeModal(modal){
modal.style.display = "none";
}
}
【问题讨论】:
-
也许这个方法对你有帮助stackoverflow.com/questions/37927657/…
标签: javascript angular youtube