Unable to set angular variable inside callback

Ruben

I'm using the google maps directions service to calculate the travel time.

this.mapsAPILoader.load().then(() => {
  const p1 = new google.maps.LatLng(50.926217, 5.342043);
  const p2 = new google.maps.LatLng(50.940525, 5.353626);

  const directionsService = new google.maps.DirectionsService();
  const request = {
    origin: p1,
    destination: p2,
    travelMode: google.maps.TravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.METRIC,
  };

  directionsService.route(request, (response, status) => {

    if (status === google.maps.DirectionsStatus.OK) {
      const point = response.routes[0].legs[0];
      // console.log(point.duration.text);
      this.travelTimeDriving = point.duration.text;
    }
  });


});

The console logs the correct driving time, but my variable this.travelTimeDriving stays empty.
I guess it had something to do with the callback function and scope but I can't fix it.
Also the route function returns void, no promise so I can't use .then()

Jaime Yule

Use NgZone to make sure the callback will be bind to the scope. Working sample:

import { Component, OnInit, NgZone } from '@angular/core';
declare const google: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  travelTimeDriving = '';

  constructor(private ngZone: NgZone) {}

  ngOnInit() {
    let mapProp = {
        center: new google.maps.LatLng(51.508742, -0.120850),
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    let map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

    const p1 = new google.maps.LatLng(50.926217, 5.342043);
    const p2 = new google.maps.LatLng(50.940525, 5.353626);

    const directionsService = new google.maps.DirectionsService();
    const request = {
      origin: p1,
      destination: p2,
      travelMode: google.maps.TravelMode.DRIVING,
      unitSystem: google.maps.UnitSystem.METRIC,
    };

    directionsService.route(request, (response, status) => this.ngZone.run(() => {

      if (status === google.maps.DirectionsStatus.OK) {
        const point = response.routes[0].legs[0];
        this.travelTimeDriving = point.duration.text;
      }
    }));
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to set variable value in callback

From Dev

Unable to Set a value inside StatusCode Callback

From Dev

Angular JS: Unable to set value to ng-model variable inside ng-repeat

From Dev

Unable to find variable inside *ngFor in Angular 2

From Dev

How to set local variable inside callback function in Java / Android?

From Dev

Access value of the scope variable inside callback function in Angular2

From Dev

Unable to get the accurate value of shell variable set inside a pipe

From Dev

How to set a value to a global variable inside a function in javascript/angular?

From Dev

SET a variable inside a CTE

From Dev

SET a variable inside a CTE

From Dev

Pass this variable to angular $http callback

From Dev

Pass this variable to angular $http callback

From Dev

Angular2 - Component variable value set on callback function not updated in template

From Dev

Access local variable inside a callback function

From Dev

boot() unable to use variable within creating() callback

From Dev

Rebinding this on angular callback from inside link funtion

From Dev

Calling function inside callback throws error in Angular

From Dev

How to set variable before callback is called?

From Dev

AngularJS: Set private service variable in callback

From Dev

Set Variable Inside Xeditable Row

From Dev

awk + set variable inside the awk

From Dev

Set scale for bc inside a variable

From Dev

PHP Unable to set a session variable

From Dev

Set variable in angular

From Dev

updating variable in http request callback in angular

From Dev

Unable to modify active record inside of net/http callback

From Dev

angular form validation inside callback function - angular 1.2.5

From Dev

variable inside a string in Angular / typescript

From Dev

Unable to set the http defaults inside the angularjs factory

Related Related

  1. 1

    Unable to set variable value in callback

  2. 2

    Unable to Set a value inside StatusCode Callback

  3. 3

    Angular JS: Unable to set value to ng-model variable inside ng-repeat

  4. 4

    Unable to find variable inside *ngFor in Angular 2

  5. 5

    How to set local variable inside callback function in Java / Android?

  6. 6

    Access value of the scope variable inside callback function in Angular2

  7. 7

    Unable to get the accurate value of shell variable set inside a pipe

  8. 8

    How to set a value to a global variable inside a function in javascript/angular?

  9. 9

    SET a variable inside a CTE

  10. 10

    SET a variable inside a CTE

  11. 11

    Pass this variable to angular $http callback

  12. 12

    Pass this variable to angular $http callback

  13. 13

    Angular2 - Component variable value set on callback function not updated in template

  14. 14

    Access local variable inside a callback function

  15. 15

    boot() unable to use variable within creating() callback

  16. 16

    Rebinding this on angular callback from inside link funtion

  17. 17

    Calling function inside callback throws error in Angular

  18. 18

    How to set variable before callback is called?

  19. 19

    AngularJS: Set private service variable in callback

  20. 20

    Set Variable Inside Xeditable Row

  21. 21

    awk + set variable inside the awk

  22. 22

    Set scale for bc inside a variable

  23. 23

    PHP Unable to set a session variable

  24. 24

    Set variable in angular

  25. 25

    updating variable in http request callback in angular

  26. 26

    Unable to modify active record inside of net/http callback

  27. 27

    angular form validation inside callback function - angular 1.2.5

  28. 28

    variable inside a string in Angular / typescript

  29. 29

    Unable to set the http defaults inside the angularjs factory

HotTag

Archive