angular - How can I get a component instance from within an event handler in Angular?
问题描述
I have a dynamic component that I need to get a reference to in the event handler function. I can't use ViewChild
because these components are dynamic and inside templates, etc.
<example #e (click)="onExample(e)" />
When I do the above e
is undefined
. How can I get the reference to the component itself (not the element) from within the handler?
解决方案
For projected content you can use ContentChild instead of ViewChild, maybe that fits your case - see docs: https://angular.io/api/core/ContentChild.
This is a good article on the topic: https://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/
推荐阅读
- c# - 在 XElement 中使用“for”循环
- javascript - 检查所有/总和值 - 如何将跨度放在输入中
- java - 在 Java 属性文件中使用值作为键
- java - 将具有负值的 byte[] 转换为 String
- python - 从 oracle (plsql) 运行 .py
- python - ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all():返回具有空值的列
- mongodb - Combine MongoDb clients: pyMongo and MongoEngine
- java - 来自 Lombok 的`@Setter` 的用法
- c# - 如何使用 Microsoft.Management.Infrastructure (MMI) 而不是 System.Management 进行本地 WMI 查询
- php - 复选框使选择输入使用 jquery 所需的 foreach