angularjs - 将对象作为 ng-map 标记的一部分传递

标签 angularjs google-maps ng-map

在带有 Angular JS 的 ng-map 中使用标记

<ng-map zoom-to-include-markers="auto" default-style="false" class="myMap">
    <marker ng-repeat="Customer in ListForDisplay"  position="{{Customer.location.lat}},{{Customer.location.lng}}" icon="{{Customer.icon}}" clickedaddress="{{Customer.address}}" clickedextras="{{Customer.extrasString}}" data="{{Customer}}"  on-click="markerSingleClick($event)" on-dblclick="markerDoubleClick($event)"></marker>
</ng-map>

我可以访问 Controller 中的字符串变量,但对象数据在调试 session 中仍然保持未定义状态:

$scope.markerSingleClick = function (event) {
     var clickedaddress = this.clickedaddress;
     var clickedextras = this.clickedextras;
     var data = this.data;

有没有办法将整个对象作为标记的一部分传递,而不是单个字符串属性

最佳答案

要通过标记on-click事件将当前对象作为参数传递,请替换

on-click="markerSingleClick($event)"

on-click="markerSingleClick({{Customer}})"

然后更新markerSingleClick函数:

$scope.markerSingleClick= function (event,customer) {
    //...          
}; 

工作示例

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,city) {
              alert(JSON.stringify(city));
              //console.log(city);
        };
    });
<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">
        <ng-map zoom="5" center="59.339025, 18.065818">            
            <marker ng-repeat="c in cities" position="{{c.pos}}" title="{{c.name}}" id="{{c.id}}" on-click="showInfo({{c}})">
            </marker>
        </ng-map>
    </div>


另一种选择是将对象标识符作为参数传递,例如其索引:

<marker ng-repeat="c in cities" on-click="showInfo($index)" position="{{c.pos}}" title="{{c.name}}" id="{{c.id}}">
</marker>

然后可以这样确定当前对象:

$scope.showInfo = function (event,index) {
     var currentCity = $scope.cities[index]; 
     //console.log(currentCity);
};

关于angularjs - 将对象作为 ng-map 标记的一部分传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39813461/

相关文章:

javascript - AngularJS $http.post 和 PHP

javascript - 如何在 AngularJS 中进行 HTTP 链接

angularjs - 没有 url 的 UI 路由器抽象状态

javascript - KMZ 文件会加载到 Google 地球,但不会加载到 Google map

javascript - 从 ngMap 自动完成获取坐标

javascript - 带有参数(托管图像的 URL)的 Ng map 标记图标不起作用

javascript - $window.print(),在angular js中不打印更新后的模型数据

android - 发布版本中的 Google Maps API key 不起作用

google-maps - 使用 googlemaps javascript API 时,Typescript 错误在 ionic2 中找不到名称 'google'

angularjs - ng-map :Can't get map object into the controller