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
  • 412
  • 1
  • 6
  • 23
Reza Mazarlou
  • 2,986
  • 4
  • 22
  • 31

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,472
  • 3
  • 16
  • 22