In my Ionic 2 TypeScript file, I am facing an issue with setting the value of a variable from another method. When I close the modal, I get undefined as the value.
I'm encountering difficulty in setting the value for coord.
export class RegisterMapPage {
..necessary variables declared here..
public coord: any;
constructor(
....
) {}
ionViewDidLoad() {
this.initMap()
}
initMap() {
this.geolocation.getCurrentPosition().then((position) => {
// // // ..... All map-related code goes here....
google.maps.event.addListener(marker, 'dragend', function () {
this.coord = marker.getPosition().lat() + ', ' + marker.getPosition().lng();
console.log(this.coord); // prints perfectly at this point.
});
}, (err) => {
console.log(err);
});
}
chooseCoord() {
console.log(this.coord); // why is it undefined??
this.viewCtrl.dismiss(this.coords);
}
}
Even though I update the variable value of coord during the marker drag event, it shows up as undefined when I try to print it out. Can someone assist me with fixing this issue?
Thank you.