내 angular2 응용 프로그램에서 ip를 기반으로 장치의 위도와 경도를 얻으려고합니다. 여기 방법 :Typscript : 변수의 범위
getGeoLocation(){
this._http.get(IP_to_GeoLocation_uri).subscribe(res =>{
this.lat=res.json().lat;
this.lon=res.json().lon;
/* console.log("lat: "+this.lat+" long: "+ this.lon); */});
}
과 console.log
결과는 getGeoLocation()
이 잘 작동하는지 보여줍니다. 그러나 다른 메서드에서이 메서드를 사용하려고하면 this.lat
및 this.lon
은 undefined
입니다. 여기 내 방법 : 그 예상
getWeatherForcastWithLocation(){
this.getGeoLocation();
console.log("lat: "+this.lat+" long: "+ this.lon);
bla..bla..bla...
}
의 this.getGeoLocation();
를 호출하여 그것은 나를 위해 lon
및 lat
을 설정합니다. 그러나 콘솔에 그들은 undefined
라고합니다. 코드에서 무엇이 잘못 되었습니까?
'get'이 (가) 비동기이므로 정의되지 않았습니다. HTTP 응답을받을 때까지 설정되지 않습니다. – cartant
응답으로 변수가 아닌 객체를 얻습니다. 권리? – micronyks
페이지에 표시하거나 추가 코드에서 사용하고 싶습니까? – micronyks