One of the challenges in my component is preserving context when passing a callback as a parameter to a method.
sendDeleteRequest(btn: MatButton, cbk: any) {
cbk()()
}
f() {
console.log(this.owner)
}
To address this, I pass the callback from the component template like this:
<button mat-raised-button (click)="sendDeleteRequest(deleteCredBtn, f)" color="warn" #deleteCredBtn="matButton">Delete</button>
However, calling the f
function results in a
ERROR TypeError: this is undefined
.
I have attempted several approaches to resolve this issue:
- Saving
this
in aself
variable within thef
function to refer toself.owner
- Returning a lambda function from the
f
function - Using
bind(this)
when invokingcbk
fromsendDeleteRequest
Unfortunately, none of these methods have successfully preserved the context.