将对象作为 ng-map 标记的一部分传递
Pass an object as part of ng-map marker
将 ng-map
中的标记与 angular JS
结合使用
<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>
我可以访问控制器中的字符串变量,但是对象 data 在调试会话中仍然未定义:
$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>
另一种选择是将对象 identifier 作为参数传递,例如它的索引:
<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);
};
将 ng-map
中的标记与 angular JS
<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>
我可以访问控制器中的字符串变量,但是对象 data 在调试会话中仍然未定义:
$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>
另一种选择是将对象 identifier 作为参数传递,例如它的索引:
<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);
};