2
我試圖使用Weakmap()
調用控制器中的服務,但它給出的錯誤爲Cannot read property 'getWeather' of undefined
。下面是我的代碼:使用ES6在AngularJs中的控制器中的服務調用
const SERVICE = new WeakMap();
export default class WeatherController {
constructor(apiService) {
SERVICE.set(this, apiService);
if (navigator.geolocation) {
navigator.geolocation.watchPosition(this.geoSuccess, this.geoFailed);
}
}
geoSuccess(position) {
// This gives an error mentioned above
SERVICE.get(this).getWeather(position.coords.latitude, position.coords.longitude);
}
geoFailed(err) {
console.log('Error:', err);
}
}
WeatherController.$inject = ['apiService'];
的可能的複製[如何訪問正確的\'這\ '/回調裏面的回調?](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback) – zeroflagL