我得到這個代碼角2 - 如何嵌入YouTube視頻
<div *ngIf="topic.video">
<div class="video-container">
<iframe width="300" height="169" src="topic.video.url" style="border:0"></iframe>
</div>
</div>
問題:角將輸出src="localhost://8001"
代替src="https://www.youtube.com/embed/hr4BbdUiTUM"
出了什麼問題嗎?
更新2
這是岡特的答案後得到的。
import { Component, OnInit, Pipe, Sanitizer } from '@angular/core';
import { DataService } from '../../shared/data';
@Component({
template: `
<div class="subheader">Feed</div>
<div *ngFor="let topic of topics; let index = index; trackBy: trackByFn">
<div *ngIf="topic.type == 'discussion'">
<div *ngIf="topic.video">
<div class="video-container">
<iframe src="{{sanitizer.bypassSecurityTrustResourceUrl(topic.video.url)}}" ></iframe>
</div>
</div>
</div>
</div>
`
})
export class CompanyComponent implements OnInit {
constructor(
private sanitizer:Sanitizer,
private dataService: DataService
) {}
ngOnInit() {
this.topics = this.dataService.topics;
}
}
我仍然得到這個錯誤
company.ts?ac6a:121 Error: Uncaught (in promise): Error: Error in ./CompanyComponent class CompanyComponent - inline template:29:41 caused by: unsafe value used in a resource URL context (see http://g.co/ng/security#xss)
Error: unsafe value used in a resource URL context (see
使用下面我回答中鏈接到的答案中所示的管道告訴Angular衛生洗滌劑它應該將URL視爲安全。 –