admin管理员组文章数量:1356294
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>
本文标签:
版权声明:本文标题:javascript - Im trying to dynamically change the url of an iframe but Im getting an Error: Unsafe value used in resource URL - S 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743961480a2569111.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论