Reputation: 11435
I got this code
<div *ngIf="topic.video">
<div class="video-container">
<iframe width="300" height="169" src="topic.video.url" style="border:0"></iframe>
</div>
</div>
problem: angular will output src="localhost://8001"
instead of src="https://www.youtube.com/embed/hr4BbdUiTUM"
What could be wrong here?
UPDATE 2
This is what got after Gunter's answer.
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;
}
}
I still got this error
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
Upvotes: 6
Views: 2883
Reputation: 657048
If you want to get the URL from a variable you need to enable binding by using []
or {{}}
(never both at the same time, {{}}
only works for strings, not for objects or arrays):
[src]="topic.video.url"
or
src="{{topic.video.url}}"
If the URL gets removed by the DOM sanitizer, you can use a pipe like shown in In RC.1 some styles can't be added using binding syntax
import { Pipe, DomSanitizer } from '@angular/core';
@Pipe({name: 'safeResourceUrl'})
export class SafeResourceUrl {
constructor(private sanitizer:DomSanitizer){}
transform(url) {
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
}
[src]="topic.video.url | safeResourceUrl"
You can also just apply
this.myUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.topic.video.url);
and bind to this instead
[src]="myUrl"
but the pipe is more reusable.
Upvotes: 5