How to define the right scope context (this) while passing a callback function to a component

How to define the right scope context (this) while passing a callback function to a component


A super common situation is to pass a callback function to component.

Template

 

Controller

 

The component takes the callback in input

In this scenario the method this.apicall(value) would fail since “this” is not what we expect.
To be sure that “this” refers to the controller’s scope, we can add a simple line on its constructor:

 

Solution


 

Leave a Reply

Your email address will not be published. Required fields are marked *