I've built a button component where one of its possible Inputs is the click handler. The problem is that the function that is passed to this handler is not in the correct context, when attempting to call the service that is injected into the parent component.
Parent Component Template
<app-vv-button
[buttonText]="'Click me'"
[type]="'primary'"
[isLoading]="isLoading"
[handler]="doFoo"></app-vv-button>
Child Component Template
<button class="btn btn--{{type}}" (click)="handler()">
<span *ngIf="!isLoading">{{buttonText}}</span>
<div class="loader" *ngIf="isLoading">Loading...</div>
</button>
Handler Function
public async doFoo() {
this.isLoading = true;
await this.barService.someFunction();
this.isLoading = false;
}
Error:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'someFunction' of undefined
TypeError: Cannot read property 'someFunction' of undefined
at VvButtonComponent.eval (VM380 app.component.ts:61)