I have many items created via *ngFor
:
<my-item *ngFor="let item of myArray" [p]="item" > </my-item>
I can listen to a click
event via
<my-item ... (click)="doWork(item) > </my-item>
But I don't want to attach so many event handlers ( since I have many components on that ngFor
).
I was wondering if I can attach an event handler to the window
object(only once) ( via @HostListener
) and then to analyze the target
property of event
and to get the actual Entity
that was clicked on - but I had no success.
Question:
I've made this plunker which already listens to a window
click
.
But How can I get the actual reference to the item
( in *ngFor="let item of myArray"
) that was clicked ?
In other words :
export class App {
myArray:Person[] = new Array<Person>();
@HostListener("window:click", ['$event'])
onWindowclick() {
console.log('click')
// how can I get a reference to the currently clicked item (as a `Person` entity) ?
}
Nb I prefer not to attach an Id and then to search that Id in an array