[英]Angular2: How to change iFrame src?
I am new to Angular2 and trying to change iframe's url on click of a button. 我是Angular2的新手,并尝试通过点击按钮更改iframe的网址。 Got passed couple of hurdles like
safe url
and setting the src for iframe
. 通过了几个障碍,比如
safe url
和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? 如何根据按钮ID更改iFrame网址?
HTML 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);
}
no need to deal with the event for such a thing as you can access component properties from the 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>
first_url = "some url";
second_url = "some other url":
current_url: SafeUrl;
updateSrc(url) {
this.current_url=this.sanitizer.bypassSecurityTrustResourceUrl(url)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.