Variable resetting to null?


I have a simple page with an input and two buttons. These buttons each have a click function: getLocation() and test().

The input has an ngModel of latLng

When I run getLocation() I set the this.latLng variable to the coordinates returned. I do a console.log and see that it’s working correctly.

But then I run the test() function, which logs the exact same variable (this.latLng) and it’s null again!

What am I missing?


I figured it out… When I was using this.latLng inside navigator.geolocation.getCurrentPosition I was in a different scope.

I changed it to:

And it works fine :slightly_smiling: