How do I pass a value from a ui-gmap-windows InfoWindow/Marker to ui-sref?
Asked Answered
B

3

7

I am trying to create a link inside an InfoWindow on a Google Map, using the angular-google-maps module's ui-gmap-windows.

In my HTML template, I have:

<ui-gmap-google-map center='main.map.center' zoom='main.map.zoom' options='main.map.options'>
     <ui-gmap-markers models="main.markers" coords="'self'" icon="'icon'" options="'options'" click="'onClick'" fit="true">
         <ui-gmap-windows show="'showWindow'" closeClick="'closeClick'" ng-cloak>
             <div class="mapinfowindow" data-ui-sref="display({id: id})">                                                                      
                 <span class="itemname" data-ng-non-bindable>{{ title }}</span>
             </div>
         </ui-gmap-windows>
     </ui-gmap-markers>
 </ui-gmap-google-map>

In my controller, I have:

uiGmapGoogleMapApi.then(function(maps) {
  vm.itemlist = search.getItemList();                                                                                                                   
  var markers = [];
  _.each(vm.itemlist,function(item){
    search.getGeometry(item.href).then(function(marker) {
      marker.title      = item.en;
      marker.id         = item.href;
      marker.showWindow = false;
      marker.options    = {
                            title: item.en,
                            icon: markericon.normal
                          };
      marker.onClick    = function() { vm.markerClick(marker); };
      marker.closeClick = function() { vm.markerCloseClick(marker); };
      markers.push(marker);
    });
  });
  vm.markers = markers;
});

Note that I'm using the 'controller as' syntax, so the vm.markers in the controller appears as main.markers in the html template.

The problem I'm seeing is that the data-ui-sref="display({id: id})" in the html changes the state to the 'display' page, but does not push through the id as a $stateParams value, which is obviously not good, as I won't know what to display..

I have another link to the same page, created outside the InfoWindow (in a list of results), and the does push through the id value:

<div data-ng-repeat="entry in main.itemlist">
    <div data-ui-sref="display({id: entry.id})">

Any help with getting the InfoWindow's link working will be very much appreciated.

Bennybenoit answered 5/12, 2014 at 3:42 Comment(3)
You might use a factory to keep $location params in order, or create a filter/decorator, for each state change occurrence.Tadeas
Have you verified that id is defined within the scope for .mapinfowindow? Add {{id}} in the span or something to see if it's definitely available.Appointee
Unless I put it inside a ng-non-bindable, nothing from the <ui-gmap-windows> scope seems to be available. But if I set ng-non-bindable, I can't use the values in ui-sref... O_oBennybenoit
B
12

Here is how I ended up solving this issue, based on info found in https://github.com/angular-ui/angular-google-maps/issues/884 :

I created a template and separate controller for the InfoWindow, and changed my HTML to:

<ui-gmap-google-map center='main.map.center' zoom='main.map.zoom' options='main.map.options'>
    <ui-gmap-markers models="main.markers" coords="'self'" icon="'icon'" options="'options'" click="'onClick'" fit="true">
        <ui-gmap-windows show="'showWindow'" closeClick="'closeClick'" templateUrl="'templateUrl'" templateParameter="'templateParameter'" ng-cloak>
        </ui-gmap-windows>
    </ui-gmap-markers>
</ui-gmap-google-map>

As you can see, there are two new parameters now: templateUrl and templateParameter.

In the main controller, I feed in the info I need for the template:

...
marker.templateUrl = templateUrl;
marker.templateParameter = {
                             id:    item.id,
                             title: item.name,
                             href:  item.href,
                             img:   vm.lookup_extphoto[item.href] //getting a photo externally
                            };
...

In the InfoWindow's template, I have:

<div data-ng-controller="infowindowTemplateController">
    <div class="mapinfowindow" data-ui-sref="display({id: parameter.id})">
        <div class="previewimage-container">
            <img data-ng-attr-src="{{:: parameter.img }}">
        </div>
        <span>{{:: parameter.title }}</span>
    </div>          
</div>

The infowindowTemplateController itself is pretty much empty:

(function(){
    'use strict';

     angular
         .module('myapp')
         .controller('infowindowTemplateController', infowindowTemplateController);

         infowindowTemplateController.$inject=['$scope'];
         function infowindowTemplateController ($scope) {
         }
})();
Bennybenoit answered 5/12, 2014 at 18:22 Comment(0)
A
2

If id is definitely available within the scope acting on .mapinfowindow you could try:

<div class="mapinfowindow" data-ui-sref="display({id: '{{id}}'})"> 

(Source: https://github.com/angular-ui/ui-router/issues/395#issuecomment-59136525)

If that doesn't work, you'll probably need a custom directive.

Appointee answered 5/12, 2014 at 10:18 Comment(3)
Close - this worked, and you don't need the single-quotes, either: <div class="mapinfowindow" data-ng-non-bindable data-ui-sref="display({id: {{id}} })"> The data-ng-non-bindable seems to be required.Bennybenoit
Actually, now that I've tested it - the id does appear in the html source, but the ui-sref doesn't fire, probably because of the very same non-bindable param..Bennybenoit
A possible solution that I'm looking into now: github.com/angular-ui/angular-google-maps/issues/884Bennybenoit
O
2

Use $parent to get the value.

Template:

<ui-gmap-marker ng-repeat="marker in markers"
                   coords="marker.coords">
    <ui-gmap-window ng-cloak>
        <a ui-sref="details({ id : $parent.marker.id })">Go!</a>
    </ui-gmap-window>
</ui-gmap-marker>

Controller:

for (var i = 0; i < mArray.length; i++) {
    var marker = {
        id            : mArray[i]._id,
        coords        : {
            latitude  : mArray[i].address.latitude,
            longitude : mArray[i].address.longitude
        }
    };
    $scope.markers.push(marker);
}
Ortiz answered 30/6, 2015 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.