The gist of what I'm trying to do, is create a generic child component that takes as an argument a list of actions of some type. Those actions would be dynamically generated on the child component as buttons and emit an event that would be caught by the parent and invoke the appropriate method there.
I'm trying to avoid just passing in the funciton bound to the parent as it doesnt feel right.
Ideally the parent would pass down data like:
[{ name: 'Edit', method: 'onEdit' }, { name: 'Delete', method: 'onDelete' }]
The child component has an emitter like:
@Output() actionEvent: EventEmitter<MyGreatEvent> = new EventEmitter();
and generates buttons like:
<button mat-menu-item *ngFor="let action of actions" (click)="actionEvent.emit({method: action.method, item: row})">
{{action.name}}
</button>
where row is the data from a mat-table row.
And then catch it in the parent via event binding with something like:
handleEvent(event: MyGreatEvent): void {
// ???
// profit
}
I've tried a handful of approaches, but always lose track of the lexical this, and I end up falling back to vanilla JS to try to solve the problem. Basically everything I can find about emitters describes a 1-to-1 relationship between an event emitter and a method. Trying to get generic but still typescript-y. I feel like I'm missing something. Any help is greatly appreciated!
Edit: I created a Stackblitz to hopefully illustrate my problem.