如何在 Angular 组件中创建 HtmlElement 引用? [英] How do I create an HtmlElement Reference in an Angular component?
问题描述
我有一个组件 MyNodeComponent
,它将 target
HTML 元素作为 Angular 7.2.15 中的 input 对象的一部分代码>:
@Component({选择器:'我的节点',templateUrl: './my-node.component.html'})导出类 MyNodeComponent 实现 OnInit、OnChanges、AfterViewInit {@Input() inputObject: [{target: HTMLElement, desc: string}];...}
问题是我不确定如何以动态方式将 HTML DOM 节点发送到目标.尤其是在具有不同层次关系的页面上可能有多个 MyNode 实例:
<my-node [inputObject]="inputObjectDefinition"></my-node><!-- target 应该指的是 target1 --><p id="target1" #target1>你好</p><div id="target2" #target2>
我将如何定义 inputObjectDefinition
以包含来自打字稿组件内部对 target1 和 target2 的引用?我是否使用 document.getElementById(它一直返回 null,但我可能使用错误)?其他方式?
回答强制性的你为什么要这样做?"问题实际上 MyNodeComponent 被用于将 dom 节点发送到 html2Canvas 库,以便我可以将页面的一部分或部分呈现为图像.
看看 Angular 的 ElementRef (https://angular.io/api/core/ElementRef).这使您可以访问 DOM 中的 Element.
你可以在这里看到它的实际效果;https://stackblitz.com/edit/angular-elementref-example
在你的情况下,你会;
@ViewChild("target1", {read: ElementRef, static: true}) target1ref: ElementRef;//获取#target1@ViewChild("target2", {read: ElementRef, static: true}) target2ref: ElementRef;//获取#target2ngAfterViewInit(): 无效 {console.log(this.target1ref.nativeElement);console.log(this.target2ref.nativeElement);}
如果你想动态选择这些,你可以通过使用 ViewChildren 引用一个指令来实现.
I have a component MyNodeComponent
that takes a target
HTML element as part of the input object in my Angular 7.2.15
:
@Component({
selector: 'my-node',
templateUrl: './my-node.component.html'
})
export class MyNodeComponent implements OnInit, OnChanges, AfterViewInit {
@Input() inputObject: [{target: HTMLElement, desc: string}];
...
}
The problem is I am not sure how to send an HTML DOM node to target in a dynamic fashion. Especially as there could be multiple instances of MyNode on a page with different hierarchical relationships:
<body>
<my-node [inputObject]="inputObjectDefinition"></my-node> <!-- target should refer to target1 -->
<p id="target1" #target1>Hello</p>
<div id="target2" #target2>
</div>
</body>
How would I define inputObjectDefinition
to contain references to target1 and target2 from inside a typescript conmponent? Do I use document.getElementById (it keeps returning null but I may be using it wrong)? Some other way?
To answer the mandatory "why are you doing this?" question in reality the MyNodeComponent is being used to send a dom node to the html2Canvas library so that I can render part or parts of the page to an image.
Take a look at Angular's ElementRef (https://angular.io/api/core/ElementRef). This gives you access to the Element in the DOM.
You can see this in action here; https://stackblitz.com/edit/angular-elementref-example
In your case, you would;
@ViewChild("target1", {read: ElementRef, static: true}) target1ref: ElementRef; // gets #target1
@ViewChild("target2", {read: ElementRef, static: true}) target2ref: ElementRef; // gets #target2
ngAfterViewInit(): void {
console.log(this.target1ref.nativeElement);
console.log(this.target2ref.nativeElement);
}
If you want to select these dynamically, you can do so by referencing say a Directive using ViewChildren instead.
这篇关于如何在 Angular 组件中创建 HtmlElement 引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!