Saturday, January 27, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...