Krunal Pawar
Krunal Pawar

Reputation: 37

ng-click inside ui-gmap-windows not working

ng-click inside ui-gmap-windows not working... Here is codepen link http://codepen.io/aoakeson/pen/ZYLJeyhttp://codepen.io/aoakeson/pen/ZYLJey

Any suggestion as how this issue is to be solved....

Here is html code:

<ui-gmap-google-map center='map.center' zoom='map.zoom' draggable="true">
             <ui-gmap-markers models="randomMarkers" coords="'self'" icon="'icon'" click="'onClick'">

                 <ui-gmap-windows show="show" ng-cloak>
                     <div class="lp_tuxtla" >
        <div >
            <a ng-click="nextpage()">click here</a>
            <h3 ng-non-bindable >{{title}}</h3>
            <h4 ng-non-bindable>{{loc}}</h4>
        </div>
        <span class="right_arw"></span>
        <span class="down_arw"></span>
    </div>

          <!--  <div ng-non-bindable >{{title}}</div>-->
        </ui-gmap-windows>

    </ui-gmap-markers>
            </ui-gmap-google-map>

Here javascript code:

$scope.map = {center: {latitude: 40.1451, longitude: -99.6680 }, zoom: 4, bounds: {}};
    $scope.options = {scrollwheel: false};
    var createRandomMarker = function (i, bounds, idKey) {
        var lat_min = bounds.southwest.latitude,
            lat_range = bounds.northeast.latitude - lat_min,
            lng_min = bounds.southwest.longitude,
            lng_range = bounds.northeast.longitude - lng_min;

        if (idKey == null) {
            idKey = "id";
        }

        var latitude = lat_min + (Math.random() * lat_range);
        var longitude = lng_min + (Math.random() * lng_range);
        var ret = {
            latitude: latitude,
            longitude: longitude,
            title: 'm' + i,
            show: false
        };
        ret.onClick = function() {
            console.log("Clicked!");
            ret.show = !ret.show;
        };
        ret[idKey] = i;
        return ret;
    };
    $scope.randomMarkers = [];
    // Get the bounds from the map once it's loaded
    $scope.$watch(function() { return $scope.map.bounds; }, function(nv, ov) {
        // Only need to regenerate once
        if (!ov.southwest && nv.southwest) {
            var markers = [];
            for (var i = 0; i < 50; i++) {
                markers.push(createRandomMarker(i, $scope.map.bounds))
            }
            $scope.randomMarkers = markers;
        }
    }, true);

Upvotes: 1

Views: 2188

Answers (3)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59338

Most likely it is the same issue that has been discussed in this thread. Apparently it occurs since nextpage event declared in controller scope is not accessible from the ui-gmap-windows child scope.

The following solution worked for me:

First we need to introduce an additional controller:

appMaps.controller('infoWindowCtrl', function($scope) {
    $scope.showInfo = function() {
        console.log('Button clicked!');
    }
});

and then specify the following layout:

<ui-gmap-windows show="show">
      <div ng-controller="infoWindowCtrl">
          <span ng-non-bindable>{{title}}</span>
          <button ng-click="showInfo()">Show info</button>
       </div>
</ui-gmap-windows>

Working example

var appMaps = angular.module('appMaps', ['uiGmapgoogle-maps']);
appMaps.controller('mainCtrl', function($scope,uiGmapIsReady) {
    $scope.map = { center: { latitude: 40.1451, longitude: -99.6680 }, zoom: 4, bounds: {} };
    $scope.options = { scrollwheel: false };

    var getRandomLat = function() {
        return Math.random() * (90.0 + 90.0) - 90.0;
    };
    var getRandomLng = function () {
        return Math.random() * (180.0 + 180.0) - 180.0;
    };

    var createRandomMarker = function(i) {
        var ret = {
            latitude: getRandomLat(),
            longitude: getRandomLng(),
            title: 'm' + i,
            show: false,
            id: i
        };
        return ret;
    };
    $scope.onClick = function(marker, eventName, model) {
        logInfo("Marker clicked!");
        model.show = !model.show;
    };
    $scope.markers = [];
    for (var i = 0; i < 200; i++) {
        $scope.markers.push(createRandomMarker(i));
    }

});

appMaps.controller('infoWindowCtrl', function($scope) {
    $scope.showInfo = function() {
        logInfo('Button clicked!');
    }
});


function logInfo(message){
   console.log(message);
   document.getElementById('output').innerHTML += message;
}
html, body, #map_canvas {
    height: 100%;
    width: 100%;
    margin: 0px;
}

#map_canvas {
    position: relative;
}

.angular-google-map-container {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
}
<script src="https://code.angularjs.org/1.3.14/angular.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
<script src="http://rawgit.com/angular-ui/angular-google-maps/2.0.X/dist/angular-google-maps.js"></script>
<div ng-app="appMaps" id="map_canvas" ng-controller="mainCtrl">
        <ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="true" options="options" bounds="map.bounds">
            <ui-gmap-markers models="markers" coords="'self'" icon="'icon'" click="onClick">
                <ui-gmap-windows show="show">
                    <div ng-controller="infoWindowCtrl">
                        <span ng-non-bindable>{{title}}</span>
                        <button ng-click="showInfo()">Show info</button>
                    </div>
                </ui-gmap-windows>
            </ui-gmap-markers>
        </ui-gmap-google-map>
</div>
<pre id="output"></pre>

Plunker

Upvotes: 1

First way

You can use your $scope declared variables in ng-click using $parent:

<button ng-click="$parent.nextPage()">next</button>

Here is a working plunker.

Second way

You can assign a controller to the div containing the window.

<div ng-controller="mainCtrl">
   <button ng-click="nextPage()">next</button>
</div>

Here is another working plunker

Credits

People in Github issue

Upvotes: 3

Vinay K
Vinay K

Reputation: 5572

Somehow, its not able to reach the 'windowClicked' method in the hierarchy.

Create the method on rootScope and use it in the tempalte.

In Controller

$scope.$root.windowClicked = function () {alert('here')}

In Markup

<a ng-click="$root.windowClicked()">test</a>

Here is the updated pen. http://codepen.io/anon/pen/qEKjjb

Upvotes: 4

Related Questions