2017-01-09 11 views
0

次のAngularJSクラスがTypeScriptで記述されています。私は基本的に2つのクラス変数に現在の位置の値を割り当てようとしていますが、このエラーが発生し続けます。現在の座標でのクラス変数の値の設定

Uncaught TypeError: Cannot set property 'longitude' of undefined at SearchCtrl.setLocations

ここで私はコントローラのコードを貼り付けます。

export class SearchCtrl implements ISearchCtrl { 

    longitude: number; 
    latitude: number; 

    constructor(public NgMap) { 
     navigator.geolocation.getCurrentPosition(this.setLocations); 
    } 

    setLocations(position): void { 
     this.longitude = position.coords.longitude; 
     this.latitude = position.coords.latitude; 
    } 
} 

おかげで、

ナノ

答えて

1

変更

this.setLocations 

p => this.setLocations(p) 

または

へへ10
this.setLocations.bind(this) 
+0

2つのクラス変数が変更されておらず、定義されていないようです。 – Nano

+0

おそらく、position.coords.longitudeとposition.coords.latitudeは未定義です。デバッガを使用してください。 –

+0

ポジションが無効になっています。ありがとう! – Nano

関連する問題