2017-05-21 7 views
0

関数間の変数の共有にはほとんど問題がありません。私の場合は、最初にlocation変数を更新するための関数watchLocation()を呼び出し、setLocation()を呼び出してこの関数に位置変数を渡しても、変数はまだ空です。たぶん私は何か間違っているが、それは私には意味がある。関数間の変数の転送

私のコードはここにある:

export class LocationComponent { 

@ViewChild("mapbox") _mapbox: ElementRef; 

    player_latitude:string; 
    player_longitude:string; 

    constructor() { 
    this.player_latitude; 
    this.player_longitude; 
    } 

watchLocation() { 
     let watchId = watchLocation(
     function (loc) { 
      if (loc) { 
       this.player_latitude = loc.latitude; 
       this.player_longitude = loc.longitude; 
      } 
     }, 
     function(e){ 
      console.log("Error: " + e.message); 
     }, 
     {desiredAccuracy: 3, updateDistance: 10, minimumUpdateTime : 1000 * 20}); 
} 

setLocation() { 
    alert(this.player_latitude); 
    this._mapbox.nativeElement.setCenter({ 
         lat: this.player_latitude, 
         lng: this.player_longitude, 
         animated: true 
    }); 
    } 
} 
+2

に動作します。矢印機能を使用します。この質問は繰り返し尋ねられています。 –

答えて

関連する問題