Saturday, 27 January 2018

javascript - Scope variables in typescript

the reason why google maps is not defined there, is because it is the callback of a function, which makes 'this' not equal to the page anymore, putting it in a variable will fix the issue.



  private siteForm: FormGroup;

loadMap(){

var siteformFromPage=this.siteForm; //this should work
google.maps.event.addListener(marker, 'dragend', function(marker, siteForm){
let newlatLng = marker.latLng;
console.log(siteformFromPage);
});
}


EDIT: if you want the latest value, you could also try is this way:




  private siteForm: FormGroup;

loadMap(){
var that=this; //cache that value of the page.

google.maps.event.addListener(marker, 'dragend', function(marker, siteForm){
let newlatLng = marker.latLng;
console.log(that.siteformFromPage);
});
}

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...