0

我正在使用 ngMap 显示带有标记的地图。当他单击标记时,我想将用户重定向到 URL。要构建这个 URL,我需要一个唯一的 id,来自重复。

我有这个;

<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)。我如何做到这一点?

4

2 回答 2

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>

于 2016-10-03T08:18:19.257 回答
0

on-click 不是角度表达式,您需要使用角度表达式来评估ctrl.id
{{ctrl.id}}
或者您的意思是使用ng-click

于 2016-03-03T20:01:08.157 回答