Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
How do I use global 'window' in Angular?

I want to use "window["initMapCallback"] to call and listen for "initMapCallback" in another file, but I'm getting an error in the debug window that says

Question - How do I correctly use this as "window["initMapCallback"] = function()" is not being triggered.

Uncaught (in promise) TypeError: window.initMapCallback is not a function at initMap ((index):35:32)

<script>
  function initMap() {
    window["initMapCallback"](); // error here!
  }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=secret-key&libraries=places&callback=initMap" type="text/javascript" async defer></script>

In the component where I want to listen and receive it I have this

ngOnInit() {
  window["initMapCallback"] = function() {
    console.log('listener entered');
  };
}

If I remove the "()" from the call, there is no error but I still don't get the function to be called or entered into.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

What happens is that the external JS is ready before your Angular app and given component and the function is not yet set. It's also very wrong. Why not try to load the map js inside the Angular and trigger the callback there?

public loadScript(url: string, callback: () => void): void {
  if (document.querySelector(`script[src="${url}"]`)) {
    callback();
    return;
  }
  const script = document.createElement('script');
  script.onload = callback();
  script.src = url;
  document.body.appendChild(script);
}

And then when you need the map:

this.loadScript(
  "https://maps.googleapis.com/maps/api/js?key=secret-key&libraries=places",
  () => {
    // some callback, usually subscription in a service
  }
);

The way for another Angular file to listen is mostly with subscriptions and services, you should not use the window object for this.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda