javascript - how to use ng click and pass the id in google map pop window using angularjs? -
i have 2 stores in database , trying stores in google map marker pointing 2 stores.i have ng-click in info window pass id here ng-click not working there idea pass id through ng-click
.controller('mapctrl', [ '$scope', '$http', '$location', '$window', function ($scope, $http, $location, $window) { $http.get('****').success(function (data, dealers, response) { function initialize() { var serverdata = data; $scope.locations = []; (var = 0; < serverdata.length; i++) { var modal = [ data[i].store_name, data[i].s_location.latitude, data[i].s_location.longitude, i, 'images/arrow.svg', data[i].s_address]; $scope.locations.push(modal); } console.log($scope.locations); //--------------------------------------------------------- //console getting var locations = [ ['nokia store', '12.971599', '77.594563', '1', 'images/arrow.svg.svg', '55a78953815356700bee698f'], ['samsung store', '12.9065534', '77.5774802', '2', 'images/arrow.svg.svg', '55a786d1815356700bee6982'], ]; //---------------------------------------------------------- var map = new google.maps.map(document.getelementbyid('map'), { zoom: 13, center: new google.maps.latlng(12.9667, 77.5667), maptypeid: google.maps.maptypeid.roadmap }); var infowindow = new google.maps.infowindow(); var marker, i; (i = 0; < $scope.locations.length; i++) { //console.log($scope.locations[i][1]); marker = new google.maps.marker({ position: new google.maps.latlng($scope.locations[i][1], $scope.locations[i][2]), map: map, icon: $scope.locations[i][4], animation: google.maps.animation.drop, }); google.maps.event.addlistener(marker, 'click', (function (marker, i) { return function () { infowindow.setcontent('<div class="marker-box"><div class="marker-title">' + $scope.locations[i][0] + '</div><input type="button" value="book now" name="book now" ng-click="getid(' + $scope.locations[i][5] + ') "/></div>'); infowindow.open(map, marker); } })(marker, i)); } $scope.map = map; } $scope.getid(id) { console.log(id); } });
so in code:
google.maps.event.addlistener(marker, 'click', (function (marker, i) { return function () { infowindow.setcontent('<div class="marker-box"><div class="marker-title">' + $scope.locations[i][0] + '</div><input type="button" value="book now" name="book now" ng-click="getid(' + $scope.locations[i][5] + ') "/></div>'); infowindow.open(map, marker); } })(marker, i));
your call function not run until marker clicked. time it's clicked, scope not know "i" is. guess "i" becomes $scope.locations.length+1
.
one things can try:
put above code in secondary function, call main thread instead of running directly main thred. ie:
main code:{ //your code (i = 0; < $scope.locations.length; i++){ function_name(parameter 1, i); } function function_name= above code;
note above sudo code, please implement , try it. hope solves it.
Comments
Post a Comment