Search code examples
javascripttypescriptionic-frameworkleafletionic4

How can I call a function inside event with "this"


How can I call the function inside of this event?

myfunction() {
  alert('bla bla!');
}
this.map.on('moveend', function() {
  console.log('moveend :: ' + this.getCenter());
  this.myfunction(); // => ERROR TypeError: this.myfunction is not a function
});

Solution

  • Try the following

    this.map.on('moveend', () => {
      console.log('moveend :: ' + this.getCenter());
      this.myfunction();
    });
    

    That should work.

    Note: The issue you're facing here is scope. I would suggest doing some reading, it's a valuable concept to understand.