Once I capture the click event on a leaflet marker and go to the associated function, I loose this for current context.
Marker create code:
const marker = L.marker([lon, lat], c).addEventListener("click", this.onClickRainFall).addTo(map);
and associated function is:
private onClickRainFall(e){
var sourceUrl2 = "a.b.c.d/" + e.sourceTarget.options.properties.abc;
this.getData(sourceUrl2);
}
Here when I try to access this.getData, at runtime I get error that "ERROR TypeError: this.getData is not a function"
Function definition for getData is:
public getData(sourceUrl){
console.log('I am called');
this.dataService.getData(sourceUrl).subscribe(data => {
this.myData = JSON.parse(data);
console.log(this.myData);
});
}
Please let me know how to solve it and get the reference of this properly.
.bind(this)
likeaddEventListener("click", this.onClickRainFall.bind(this))
. There are a lot of simular questions on stackoverflow – yurzui