When passing an argument, the context is lost to the function. How can I bind it in my typescript file?
import { Component } from "@angular/core";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
name = "Angular";
getName(): string {
return this.name;
}
onAction(cb: Function) {
return cb();
}
}
<p>
{{onAction(getName)}}
</p>
Error: Cannot read property 'name' of undefined
If you make the binding in the template, then this will work.
<p>
{{onAction(getName).bind(this)}}
</p>
But I would like to make the linking in the controller.
I would be grateful for any help!