gpt4 book ai didi

javascript - Angular 2 : How to change iFrame src?

转载 作者:太空狗 更新时间:2023-10-29 17:48:28 27 4
gpt4 key购买 nike

我是 Angular2 的新手,尝试通过单击按钮更改 iframe 的 url。通过了几个障碍,例如 safe urlsetting the src for iframe。无法找到一种在单击按钮/链接时更改 url 的好方法。
如何根据按钮id更改iFrame url?
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>

组件

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

最佳答案

无需处理事件,因为您可以从模板访问组件属性...

模板:

<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>

:

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


updateSrc(url) {
this.current_url=this.sanitizer.bypassSecurityTrustResourceUrl(url)
}

关于javascript - Angular 2 : How to change iFrame src?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42213243/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com