When working with a mapboxgl map click listener in an Angular2 component, I encountered an issue regarding accessing variables outside of the click function. Despite my efforts, using this.something
did not work as expected. It seems to be a scope problem specific to mapboxgl. Here is a simplified version of the problem where the console prints out undefined
...
public map: Map;
public test: string = 'test';
initialize() {
this.map.on('click', clickListener);
}
clickListener() {
console.log(this.test); // undefined
}