[英]AngularJS ngMap parameters on-click event
我正在嘗試使用這個Angular Google地圖指令http://ngmap.github.io/ 。 我想為每個標記添加一個click事件,它會調用控制器中的一個函數,並通過標記的id。 當我在控制台中打印出id時,我得到“hq {latLng:wf,ub:undefined,pixel:undefined,xa:undefined}”,這不是id。 我的HTML是
<map center="{{map.center.latitude}},{{map.center.longitude}}" zoom="{{map.zoom}}">
<marker ng-repeat="marker in markers" position="{{marker.latitude}}, {{marker.longitude}}" icon="{{marker.icon}}" id="{{marker.id}}" on-click="goAnchor(marker.id)"></marker>
</map>
我的控制器代碼:
$scope.goAnchor = function (id) {
console.log(id);
gotoAnchor(id);
};
“this”返回您點擊的標記
$scope.goAnchor = function (event) {
console.log(this.id);
gotoAnchor(this.id);
};
HTML
<marker ng-repeat="marker in markers" position="{{marker.latitude}},{{marker.longitude}}" icon="{{marker.icon}}" id="{{marker.id}}" on-click="goAnchor($event)"></marker>
你應該在函數中使用
$scope.goAnchor = function (event) {
console.log(event.target.attributes.id.value);
gotoAnchor(event.target.attributes.id.value);
};
標記
<marker ng-repeat="marker in markers" position="{{marker.latitude}}, {{marker.longitude}}" icon="{{marker.icon}}" id="{{marker.id}}" on-click="goAnchor($event)"></marker>
我使用下面的代碼來獲取點擊標記的緯度和經度。
<ng-map zoom="12" center="{{vm.geoCenterLoc}}" style="height:500px">
<marker ng-repeat="p in vm.positions"
position="{{p.pos}}"
data="{{data[$index]}}"
on-click="myFunc($event)";
title="pos: {{p.pos}}"></marker>
</ng-map>
Javascript(在我的控制器上):
$scope.myFunc = function (evt) {
markerPosObj = {
pos: [this.getPosition().lat(), this.getPosition().lng()]
};
markerPos = [];
markerPos.push(markerPosObj);
console.log('this marker', markerPos);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.