2016-03-03 15 views
0

我正在使用ngMap來顯示帶有標記的地圖。我想將用戶重定向到一個URL,當他點擊一個標記。要建立這個網址,我需要一個唯一的ID,從重複。帶有ID的ngMap中的點擊標記?

我有這個;

<marker ng-repeat="position in ctrl.positions" position="{{position.x}}, {{position.y}}" on-click="ctrl.goto(ctrl.id)"></marker> 

在我的控制,我有

vm.clickRedirect = function(pId) { 
    console.log(pId); 
} 

我剛剛返回的對象,而實際的ID(ctrl.id)。我如何實現這一目標?

+0

究竟是什麼的問題? –

+0

我如何從我的結果(ctrl.id)中獲取id,並轉到我的單擊事件。正如所寫,當前代碼傳遞一個映射對象,而不是實際的int id。 – brother

+0

您在控制器中調用的功能在哪裏? –

回答

0

上單擊不是一個角度表達 你需要使用的角度表達來評價ctrl.id
這樣的:{{ctrl.id}}
還是你的意思使用ng-click

+0

我以前嘗試過,但它做同樣的事情:on-click =「ctrl.clickRedirect({{ctrl.id}})」 – brother

+0

ng-click根據http://stackoverflow.com/questions/24477634不會工作/ angularjs - 谷歌 - 地圖-NG-點擊裏面標記物 – brother

3

要通過對象標識符經由on-click事件的參數替換clickRedirect功能:

vm.clickRedirect = function(event,pId) { 
    console.log(pId); 
} 

工作實施例

angular.module('mapApp', ['ngMap']) 
 
    .controller('mapCtrl', function ($scope, NgMap) { 
 

 

 
     NgMap.getMap().then(function (map) { 
 
      $scope.map = map; 
 
     }); 
 
     $scope.cities = [ 
 
      { id: 1, name: 'Oslo', pos: [59.923043, 10.752839] }, 
 
      { id: 2, name: 'Stockholm', pos: [59.339025, 18.065818] }, 
 
      { id: 3, name: 'Copenhagen', pos: [55.675507, 12.574227] }, 
 
      { id: 4, name: 'Berlin', pos: [52.521248, 13.399038] }, 
 
      { id: 5, name: 'Paris', pos: [48.856127, 2.346525] } 
 
     ]; 
 

 
     $scope.showInfo = function (event,id) { 
 
       $scope.selectedCity = $scope.cities.filter(function(c){ 
 
        return c.id === id; 
 
       })[0]; 
 
     }; 
 
    });
<script src="https://code.angularjs.org/1.4.8/angular.js"></script> 
 
<script src="https://maps.googleapis.com/maps/api/js?key="></script> 
 
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script> 
 

 
<div ng-app="mapApp" ng-controller="mapCtrl"> 
 
     <div>Selected city: {{selectedCity}}</div> 
 
     <ng-map zoom="4" center="59.339025, 18.065818">    
 
      <marker ng-repeat="c in cities" position="{{c.pos}}" title="{{c.name}}" id="{{c.id}}" on-click="showInfo(c.id)"> 
 
      </marker> 
 
     </ng-map> 
 
</div>

相關問題