在Angular中插入src到iframe可以通过以下步骤实现:
<iframe id="myIframe"></iframe>
import { Component, ViewChild, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements AfterViewInit {
@ViewChild('myIframe') myIframe: any;
ngAfterViewInit() {
this.myIframe.nativeElement.src = 'https://example.com';
}
}
这样,当组件加载完成后,iframe将会显示指定URL的内容。
请注意,上述示例中使用了ViewChild装饰器来获取iframe元素的引用。这是因为在Angular中,通过模板引用变量(例如#myIframe)无法直接访问iframe元素的属性和方法。通过ViewChild装饰器,我们可以获取到iframe元素的引用,并通过nativeElement属性来访问其原生DOM对象。
推荐的腾讯云相关产品:腾讯云云服务器(CVM),产品介绍链接地址:https://cloud.tencent.com/product/cvm
领取专属 10元无门槛券
手把手带您无忧上云