【发布时间】:2021-09-01 01:24:57
【问题描述】:
组件是通过ngFor 生成的。同一个 click 事件处理程序绑定到所有这些。我想捕获用户在事件处理程序中单击的Component 作为参数。
模板
<div>
<app-my-component (click)="pageClicked(<sender>)" *ngFor="let page of pages"></app-my-component>
</div>
代码背后
pageClicked(sender: MyComponent): void {
// sender would be the COMPONENT who called this function.
}
我已经尝试过的
- 通过将组件命名为
#comp来使组件成为模板变量,我得到了一个空对象 - 使用
this关键字,this在此上下文中是父组件,通过 ngFor 生成较小的组件 - 发送
$event作为参数并通过path查找组件,我不想要html元素,我想要整个组件并访问它的公共变量和方法
最终在捕获组件后,我想将它的 [selected] Input 设置为 true 并清除最后选择的组件,如果我只能获取组件并将其存储在局部变量中,这应该不难.
【问题讨论】:
标签: angular typescript components sender