I new on Angular 2, and i want to know if it's possible invoke a child method from the current constructor.
For example, I want to call getPosition method from constructor, but throw an exception that says "getPosition is not a function".
import { Component } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { Platform } from 'ionic-angular';
import { Q } from 'q';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
private map;
constructor(public navCtrl: NavController, platform: Platform, public alertCtrl: AlertController) {
platform.ready().then(() => {
try {
let div = document.getElementById("map_canvas");
// Initialize the map view
this.map = (<any>window).plugin.google.maps.Map.getMap(div);
// Wait until the map is ready status.
this.map.addEventListener((<any>window).plugin.google.maps.event.MAP_READY, function() {
this.getPosition().then(data => {
let GOOGLE = new (<any>window).plugin.google.maps.LatLng(data.latitude, data.longitude);
this.map.setCenter(GOOGLE);
}).catch(err => {
alert(err);
});
});
} catch(err) {
alert(err);
}
}).catch(err => {
alert(err);
});
}
getPosition() {
let deferred = Q.defer();
try {
this.map.getMyLocation(location => {
deferred.resolve( {
latitude: location.latLng.lat,
longitude: location.latLng.lng
});
}, err => {
deferred.reject(err);
});
} catch(err) {
deferred.rejec(err);
}
return deferred.promise;
}
}
Change,
// Wait until the map is ready status.
this.map.addEventListener((<any>window).plugin.google.maps.event.MAP_READY, function() {
this.getPosition().then(data => {
let GOOGLE = new (<any>window).plugin.google.maps.LatLng(data.latitude, data.longitude);
this.map.setCenter(GOOGLE);
}).catch(err => {
alert(err);
});
});
to
// Wait until the map is ready status.
this.map.addEventListener((<any>window).plugin.google.maps.event.MAP_READY, ()=> {
this.getPosition().then(data => {
let GOOGLE = new (<any>window).plugin.google.maps.LatLng(data.latitude, data.longitude);
this.map.setCenter(GOOGLE);
}).catch(err => {
alert(err);
});
});
since you are using function
instead of ()=>
(fat arrow syntax) your this
is refering to your function object inside the .addEventListener
part
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments