CliffCloud / Leaflet.EasyButton

leaflet control buttons with icons and callbacks
http://cliffcloud.github.io/Leaflet.EasyButton/v1/
MIT License
524 stars 123 forks source link

Leaflet.EasyButton : call of js function #78

Closed walidaziz closed 6 years ago

walidaziz commented 6 years ago

Hi, I would like to call a method defined in my angular component class from my button. But the following code give me an 'undefined function' for myMethodToCall. How could i do that ?

Thanks

initMap() { 
this.map = L.map('map');

L.easyButton('fa-globe', function(btn, map){
    this.myMethodToCall();
      }).addTo( this.map );
}
myMethodToCall () {
     this.map....
   // some dynamic stuff (call current GPS position and setView)
}
atstp commented 6 years ago

EasyButton rebinds this in the button's handler function. (see the docs)

try avoiding the use of this:

initMap() { 
    var self = this;  // ADD THIS
    this.map = L.map('map');

    L.easyButton('fa-globe', function(btn, map){
        self.myMethodToCall();  // CALL AS self 
    }).addTo( this.map );
}
myMethodToCall () {
    // this.map....
    // some dynamic stuff (call current GPS position and setView)
}

if that doesn't help, share a minimal working example.

walidaziz commented 6 years ago

Great ! That works. Thanks