Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS ng-include inside of Google Maps InfoWindow?
    primarykey
    data
    text
    <p>I'm trying to include a template file <code>views/infowindow.html</code> as the content of my InfoWindow from service I wrote to initiate the google maps api:</p> <pre><code>for ( var count = locations.length, i = 0; i &lt; count; i++ ) { var latLng = locations[i], marker = new google.maps.Marker({ … }), infowindow = new google.maps.InfoWindow(); google.maps.event.addListener( marker, 'click', (function( marker , latLng ){ return function(){ var content = '&lt;div ng-include src="\'infowindow.html\'"&gt;&lt;/div&gt;'; infowindow.setContent( content ); infowindow.open( Map , marker ); }//return fn() })( marker , latLng ) );//addListener }//for </code></pre> <p>However, it seems that Angular is not processing <code>content</code> when it is inserted into the InfoWindow (when inspecting the code via Dev Tools, the code that gets inserted is <code>&lt;div ng-include src="'views/infowindow.html'"&gt;&lt;/div&gt;</code>).</p> <p>I was hoping Angular would pre-process my include before it was inserted into the InfoWindow, but alas no.</p> <p>Is what I'm trying to do possible?</p> <p>I'm thinking that I'll have to somehow cache the template before passing it to <code>infowindow.setContent()</code>, but I don't know how to do that (or if that's even what I <em>should</em> be doing). I would prefer to load the template on the event instead of caching and injecting it for each marker.</p> <p><strong>EDIT</strong> Looking at $templateCache and a <a href="https://stackoverflow.com/a/12346901/758177">related SO question</a>.</p> <p><strong>EDIT 2</strong> Here's a <a href="http://embed.plnkr.co/vfntxf" rel="nofollow noreferrer">plunk</a> that tries to use <code>$compile</code> (the content of InfoWindow is still <code>&lt;div id="infowindow_content" ng-include src="'infowindow.html'"&gt;&lt;/div&gt;</code>)</p> <hr /> <h2>SOLUTION</h2> <p>The basis for this came from <a href="https://stackoverflow.com/a/14228601/758177">Mark's answer below</a>. In his solution, the content for InfoWindow is compiled on first click (of any marker) but the InfoWindow does not actually open until another click on any Marker, probably because GoogleMaps is impatient.</p> <p>Moving the <code>$compile</code> outside and then passing the compiled template into <code>.addListener</code> solves this problem:</p> <pre><code>for ( … ) { … infowindow = new google.maps.InfoWindow(); scope.markers … var content = '&lt;div id="infowindow_content" ng-include src="\'infowindow.html\'"&gt;&lt;/div&gt;'; var compiled = $compile(content)(scope); google.maps.event.addListener( marker, 'click', (function( marker , scope, compiled , localLatLng ){ return function(){ scope.latLng = localLatLng;//to make data available to template scope.$apply();//must be inside write new values for each marker infowindow.setContent( compiled[0].innerHTML ); infowindow.open( Map , marker ); };//return fn() })( marker , scope, compiled , scope.markers[i].locations ) );//addListener }//for </code></pre> <p><a href="http://plnkr.co/edit/vfntxf" rel="nofollow noreferrer">Updated Plunker</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload