1

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
});
Terry
  • 63,248
  • 15
  • 96
  • 118

1 Answers1

4

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.

Dane Brouwer
  • 2,827
  • 1
  • 22
  • 30
  • 2
    If OP needs more info about what the difference is between a "function" and an "arrow function". I'd suggest checking out [Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?](https://stackoverflow.com/questions/34361379/are-arrow-functions-and-functions-equivalent-exchangeable) – 3limin4t0r Apr 03 '20 at 14:20
  • Thank you for your reply. I will have an error with "this.getCenter()" => Property 'getCenter' does not exist on type 'HomePage'. :/ – Issam Globe Apr 03 '20 at 14:25
  • It is resolved (by adding the following parameter) this.map.on('moveend', ($event) => { console.log('moveend :: ' + $event.target.getCenter()); this.myfunction(); }); Thank you very much ^^ – Issam Globe Apr 03 '20 at 14:31