Calling a method from the constructor in Angular2/IONIC2

Vladimir Venegas

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;    
  }

}
eko

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling method from constructor

From Dev

Calling virtual method from a constructor

From Javascript

Calling a class method from the constructor

From Java

Calling an overridden method from a constructor

From Java

Calling a Method from Constructor Method - Java

From Dev

Calling a constructor from method within the same class

From Dev

Safe alternative to calling of abstract method from constructor

From Dev

Calling constructor from a subclass method with a generic parameter

From Dev

Calling a method from child component in ionic 2

From Dev

Calling a method on a constructor method

From Dev

Calling a void Method into a constructor

From Dev

Calling a constructor as a method

From Dev

Calling action from constructor vs a life cycle method

From Dev

React Native Issue - Initialize state by calling another method from constructor

From Dev

Calling a method of a function constructor from another function in the global scope - Javascript

From Dev

Calling ES6 class constructor from class static method

From Dev

Calling an instance method from a lambda given as an argument to a constructor

From Dev

Calling constructor from shared method (C# to VB.Net)

From Dev

Calling private method from inside the constructor does not change values of variables

From Java

Calling Constructor from another Constructor

From Java

calling setters from a constructor

From Dev

Calling constructor with "()" is different from "{}"

From Dev

Calling a value from a constructor

From Dev

Calling a javascript constructor functions method

From Dev

Component constructor and Router life-cycle hooks not triggered when calling router.parent.navigate method from child component in Angular2 routing

From

Calling method from a Angular 2 class inside template

From Dev

Calling constructor of subclass from constructor of superclass

From Dev

Calling a protected constructor from a derived constructor

From Dev

Calling method 2 classes down from current method C#

Related Related

  1. 1

    Calling method from constructor

  2. 2

    Calling virtual method from a constructor

  3. 3

    Calling a class method from the constructor

  4. 4

    Calling an overridden method from a constructor

  5. 5

    Calling a Method from Constructor Method - Java

  6. 6

    Calling a constructor from method within the same class

  7. 7

    Safe alternative to calling of abstract method from constructor

  8. 8

    Calling constructor from a subclass method with a generic parameter

  9. 9

    Calling a method from child component in ionic 2

  10. 10

    Calling a method on a constructor method

  11. 11

    Calling a void Method into a constructor

  12. 12

    Calling a constructor as a method

  13. 13

    Calling action from constructor vs a life cycle method

  14. 14

    React Native Issue - Initialize state by calling another method from constructor

  15. 15

    Calling a method of a function constructor from another function in the global scope - Javascript

  16. 16

    Calling ES6 class constructor from class static method

  17. 17

    Calling an instance method from a lambda given as an argument to a constructor

  18. 18

    Calling constructor from shared method (C# to VB.Net)

  19. 19

    Calling private method from inside the constructor does not change values of variables

  20. 20

    Calling Constructor from another Constructor

  21. 21

    calling setters from a constructor

  22. 22

    Calling constructor with "()" is different from "{}"

  23. 23

    Calling a value from a constructor

  24. 24

    Calling a javascript constructor functions method

  25. 25

    Component constructor and Router life-cycle hooks not triggered when calling router.parent.navigate method from child component in Angular2 routing

  26. 26

    Calling method from a Angular 2 class inside template

  27. 27

    Calling constructor of subclass from constructor of superclass

  28. 28

    Calling a protected constructor from a derived constructor

  29. 29

    Calling method 2 classes down from current method C#

HotTag

Archive