Im trying to dynamically change the src of an iframe using angular 2 Ive tried
HTML
<iframe class="controls" width="580" height="780" src="{{controllerSrc}}" frameborder="0" allowfullscreen></iframe>
COMPONENT.TS
import { Component, OnInit } from '@angular/core';
declare var jQuery: any;
const $ = jQuery;
export class VideoplayerComponent implements OnInit {
controllerSrc: string;
ngOnit(){
$('.button1').click(function(){
this.controllerSrc = '/?widgets=people';
});
}
}
but Im getting an error that says
ERROR Error: unsafe value used in a resource URL context (see )
not sure how else I can do this, any help would be appreciated,
Thanks
Im trying to dynamically change the src of an iframe using angular 2 Ive tried
HTML
<iframe class="controls" width="580" height="780" src="{{controllerSrc}}" frameborder="0" allowfullscreen></iframe>
COMPONENT.TS
import { Component, OnInit } from '@angular/core';
declare var jQuery: any;
const $ = jQuery;
export class VideoplayerComponent implements OnInit {
controllerSrc: string;
ngOnit(){
$('.button1').click(function(){
this.controllerSrc = 'https://www.videoindexer.ai/embed/insights/0c7357bd5c/?widgets=people';
});
}
}
but Im getting an error that says
ERROR Error: unsafe value used in a resource URL context (see http://g.co/ng/security#xss)
not sure how else I can do this, any help would be appreciated,
Thanks
Share Improve this question edited Nov 9, 2017 at 5:14 Smokey Dawson asked Nov 9, 2017 at 4:36 Smokey DawsonSmokey Dawson 9,24022 gold badges85 silver badges162 bronze badges2 Answers
Reset to default 5No need of Jquery. Use Angular DomSanitizer
import { DomSanitizer} from '@angular/platform-browser';
export class VideoplayerComponent implements OnInit {
controllerSrc: any;
constructor(private sanitizer: DomSanitizer) {}
ngOnit(){
const url='https://www.videoindexer.ai/embed/insights/0c7357bd5c/?widgets=people';
this.controllerSrc = this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
}
<iframe class="controls" width="580" height="780" [src]="controllerSrc" frameborder="0" allowfullscreen></iframe>
Please refer to the docs Angular DomSanitizer
First of all remove jQuery from your mind,while you are learning Angular2 or +,
Otherwise, you will never know how to do it in Angular way.
Here how you can achieve same thing without jQuery
Component side :
// to remove error : Error: unsafe value used in a resource URL context
import { DomSanitizer } from '@angular/platform-browser';
export class VideoplayerComponent implements OnInit {
controllerSrc: string;
constructor(sanitizer: DomSanitizer){}
ngOnit(){
this.controllerSrc = this.getSafeUrl('https://www.videoindexer.ai/embed/insights/0c7357bd5c/?widgets=people');
}
// to remove error : Error: unsafe value used in a resource URL context
getSafeUrl(url) {
return this.sanitizer.bypassSecurityTrustResourceUrl(url)
}
changeIframe() {
this.controllerSrc = this.getSafeUrl('your-new-url');
}
}
Template side :
<button (click)='changeIframe()'></button>
<iframe class="controls" width="580" height="780" [src]="controllerSrc" frameborder="0" allowfullscreen></iframe>