我想将 HTML 元素添加到 iframe,但每次检查源代码时,都没有添加任何内容。
我的代码如下:
HTML
<div class="canvas-wrapper">
<iframe class="canvas" #canvas></iframe>
</div>
打字稿
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
@Component({
selector: 'app-canvas',
templateUrl: './canvas.component.html',
styleUrls: ['./canvas.component.scss'],
})
export class CanvasComponent implements OnInit {
@ViewChild('canvas') canvas: ElementRef;
ngOnInit(): void {
this.addHtml();
}
addHtml(): void {
const canvas = this.canvas.nativeElement.contentDocument;
canvas.appendChild('<h1>TEST CONTENT</h1>');
}
}
慕哥6287543
相关分类