8

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);
  }

1 Answer 1

8

no need to deal with the event for such a thing as you can access component 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)
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Do I still need to call this.reportUrl()? I am not using it anywhere on the template.
You put it in your code, I thought it was a side-effect, but it's not needed for this code, indeed...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.