Access and modify variable in anonymous function as parameter in other function JS/Angular

末鹿安然 提交于 2020-01-06 20:18:42

问题


I have an issue to access at some variables outside an anonymous function called in another function as parameter. This is the code :

clicked(){
    var lat;
    var lng;
    this.map.on('click',function(e) {
            var coordonneesDiv = document.getElementById("coordonnees");
            lat = e.latlng.lat;
            lng = e.latlng.lng;
            //var zoom = 18; 
            coordonneesDiv.innerHTML = "lat : " + lat + "<br/>" + "long : "+ lng;
            //Zoom lors d'un  click 
            //
            //this.map.setView([lat, lng], zoom);
    });
    this.getData(lat,lng); 
}

Is it possible to modify the lat and lng variables inside the anonymous function to use them after (in getData).

Thanks for you answers

-------- Correction -----

clicked(){
    var lat;
    var lng;
    var self = this;
    console.log(self);
    this.map.on('click',function(e,self){
            console.log(self);
            var coordonneesDiv = document.getElementById("coordonnees");
            lat = e.latlng.lat;
            lng = e.latlng.lng;
            //var zoom = 18; 
            coordonneesDiv.innerHTML = "lat : " + lat + "<br/>" + "long : "+ lng;
            //Zoom lors d'un  click 
            self.getData(lat,lng);
            //this.map.setView([lat, lng], zoom);
    });
    this.getData(lat,lng); 
}

Am I doing it wrong ?

The first log send me that the self exist. But the second not.

Sorry for my clumsiness...

来源:https://stackoverflow.com/questions/34904676/access-and-modify-variable-in-anonymous-function-as-parameter-in-other-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!