6

I use google map API in my application like this :

import url with api key in index.html :

<script src="https://maps.googleapis.com/maps/api/js?key=myKey"></script>

and in component declare google like this :

declare let google: any;

and use it in component like this :

this.map = new google.maps.Map(document.getElementById('googleMap'), this.mapProp);

How can I change API key dynamically that exist in the index.html ?

Wei-jye
  • 262
  • 1
  • 4
  • 21
Reza Mazarlou
  • 2,798
  • 4
  • 21
  • 29

1 Answers1

0

If you really need to do this, you can add the script tag directly to the DOM.

var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?key=myKey';
$("#someElement").append( script );

It will load the new code when you add it.

Code is based on https://stackoverflow.com/a/611016

waternova
  • 1,342
  • 2
  • 14
  • 21