gpt4 book ai didi

angular - ngFor youtube 链接与 Angular2 中的 Domsanitizer

转载 作者:行者123 更新时间:2023-12-02 13:37:55 27 4
gpt4 key购买 nike

我的模拟内存数据库中有 youtube 链接,我想 *ngFor 这些来自 youtube 的视频。

   let videos: any[] =[
{videoURL: "ZOd5LI4-PcM"},
{videoURL: "d6xQTf8M51A"},
{videoURL :"BIfvIdEJb0U"}

];

像这样。

我使用服务将我的组件连接到服务器,现在在 html 中,我有让v个视频。在 iframe 标签内..我做到了

<iframe src=v.videoURL></iframe>

但由于它是外部来源,他们告诉我使用 Domsanizer,但我陷入了这部分。

我不知道如何清理应该循环的链接。

constructor( private sanitizer: DomSanitizer) {
this.sanitizer.bypassSecurityTrustResourceUrl('')

<- 我不知道要在这里添加什么。

最佳答案

您可以像这样创建管道:

@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {}
transform(url) {
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
}

并按以下方式使用它:

<div *ngFor="let video of videos">
<iframe [src]="('https://www.youtube.com/embed/' + video.videoURL) | safe"></iframe>
</div>

<强> Plunker Example

另请参阅

关于angular - ngFor youtube 链接与 Angular2 中的 Domsanitizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42500324/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com