javascript - calling a function in angular controller from google api present in view -


i using google places details api. in src attribute put google api has callback named initmap here code

<div class="tab-pane active"  id="timeline">  <p class="lead">location</p>  <hr>  <div class="row">   <div class="col-md-1"></div>   <div class="col-md-8">    <h2><a href="#"></a>      <span>location <b style="color:black;"> kolkata</b></span></h2>       <p></p>       <div id="map" style="width:100%;height:400px;"></div>       <script src="https://maps.googleapis.com/maps/api/js?key=api_key&libraries=places&callback=initmap"></script>      <p></p>     </div>   </div> </div> 

in same html have written initmap function

<script> function initmap() {   var map = new google.maps.map(document.getelementbyid('map'), {       center: {lat: 22.652687, lng: 88.376882},       zoom: 15     });      var infowindow = new google.maps.infowindow();     var service = new google.maps.places.placesservice(map);      service.getdetails({       placeid: 'chijvybrt7gd-dkr6qzqfjp9zjg'     }, function(place, status) {       debugger       if (status === google.maps.places.placesservicestatus.ok) {         var marker = new google.maps.marker({           map: map,           position: place.geometry.location         });         google.maps.event.addlistener(marker, 'click', function() {           infowindow.setcontent('<div><strong>' + place.name + '</strong><br>' +             'place id: ' + place.place_id + '<br>' +             place.formatted_address + '</div>');           infowindow.open(map, this);         });       }     }); } </script> 

this works long function present inside script tag. how call initmap inside controller?

you can't call way instead load script using javascript inside controller when app loaded using document.createelement('script') hook onload event listener append inside head head tag.

most likely:

var script = document.createelement('script'); script.src = 'google api script'; script.onload = function () { // onload function $scope.onload(); $scope.$digest(); };  document.queryselector('head').appendchild(script); 

this way have control on ng stuff want everytime loads. hope helps


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -