最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2: How to change iFrame src? - Stack Overflow

programmeradmin1浏览0评论

I am new to Angular2 and trying to change iframe's url on click of a button. Got passed couple of hurdles like safe url and setting the src for iframe. Can't figure out a nice way to change the url on click of buttons/links.
How can change the iFrame url based on the button id?
HTML

<button md-button (click)="updateSrc($event)"  id="first" class="top-link">First</button>
  <button md-button  (click)="updateSrc($event)" id="second" class="top-link">Second</button>

<iframe id="frame" frameborder="0" [src]="changeUrl()"></iframe>

Component

private first_url = "some url";
private second_url = "some other url":

updateSrs(event) {
    console.log('Here');
    console.log(event.currentTarget.id);
    this.reportUrl();
  }
;
  changeUrl() {
    return this.sanitizer.bypassSecurityTrustResourceUrl(this.first_url);
  }

I am new to Angular2 and trying to change iframe's url on click of a button. Got passed couple of hurdles like safe url and setting the src for iframe. Can't figure out a nice way to change the url on click of buttons/links.
How can change the iFrame url based on the button id?
HTML

<button md-button (click)="updateSrc($event)"  id="first" class="top-link">First</button>
  <button md-button  (click)="updateSrc($event)" id="second" class="top-link">Second</button>

<iframe id="frame" frameborder="0" [src]="changeUrl()"></iframe>

Component

private first_url = "some url";
private second_url = "some other url":

updateSrs(event) {
    console.log('Here');
    console.log(event.currentTarget.id);
    this.reportUrl();
  }
;
  changeUrl() {
    return this.sanitizer.bypassSecurityTrustResourceUrl(this.first_url);
  }
Share Improve this question edited Feb 15, 2017 at 15:46 Himanshu Yadav asked Feb 13, 2017 at 20:47 Himanshu YadavHimanshu Yadav 13.6k49 gold badges170 silver badges295 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

no need to deal with the event for such a thing as you can access ponent properties from the template...

template:

<button md-button (click)="updateSrc(first_url)"  id="first" class="top-link">First</button>
<button md-button  (click)="updateSrc(second_url)" id="second" class="top-link">Second</button>

<iframe id="frame" frameborder="0" [src]="current_url"></iframe>

ts :

first_url = "some url";
second_url = "some other url":
current_url: SafeUrl;


updateSrc(url) {
    this.current_url=this.sanitizer.bypassSecurityTrustResourceUrl(url)
  }
发布评论

评论列表(0)

  1. 暂无评论