0

I would like to emit a new value to my BehaviourSubject when Google Maps Api has been loaded, but I cannot acces to class parameters, code:

private isInitialized = new BehaviorSubject<boolean>(false);

  private initialization() {
    // Load Google Maps Api script
    var script = document.createElement('script');
    script.src = 'https://maps.googleapis.com/maps/api/js?key=' + environment.googlePlaces.apiKey + '&libraries=places&callback=initMap';
    script.async = true;

    window['initMap'] = function () { debugger; this.isInitialized.next(true); }
    document.head.appendChild(script);
  }

I enter in debugger, but this is associated to something else than the class.

How can I connect this to the instance of my class?

Cedric Arnould
  • 1,991
  • 4
  • 29
  • 49
  • 1
    Does this answer your question? [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – John Montgomery Jun 17 '20 at 22:45
  • Thanks :) When I made a search, I didn't find the answer with my words, I shared the solution bellow. – Cedric Arnould Jun 17 '20 at 22:54

1 Answers1

0

replace:

window['initMap'] = function() { debugger; this.isInitialized.next(true); }

by:

 window['initMap'] = () => { debugger; this.isInitialized.next(true); }
Cedric Arnould
  • 1,991
  • 4
  • 29
  • 49