Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Variable Value outside of Object Property

I’m still a newbie to angular and js but I’m trying to setup a interactive world map where you click one country and get some values.
I’m using jsvectormap and angular. Jsvectormap provides the onRegionClick function. Within that function i set the country variable to the clicked country which works fine. but i cant access this value outside of the object property.

See the code in this demo: https://stackblitz.com/edit/angular-interactiveworldmap

Any ideas how this could work? Thanks in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Please change it to an arrow function.

Unlike regular functions, arrow functions do not have their own this.
The value of this inside an arrow function remains the same throughout
the lifecycle of the function and is always bound to the value of this
in the closest non-arrow parent function.

To learn more visit here

onRegionClick: () => {
    var selectedCountry = map.getSelectedRegions();
    console.log('selectedCountry: ', selectedCountry);
    this.country = selectedCountry;
    console.log('this.country: ', this.country);
    //this.getCountry();
  },

stackblitz

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading