我正在使用AngularJs。我正在显示带有标记的谷歌地图。点击标记后,我显示点击的标记详细信息。
这是我的Controller.js
function createMarker(latitude,longitude,name,address,locationId,sid){
var marker = new google.maps.Marker({
map: $scope.map,
position: new google.maps.LatLng(latitude,longitude),
title: name
});
$scope.leadsForMap=[];
$scope.location={};
google.maps.event.addListener(marker, 'click', function() {
$scope.location = {
name: name,
sid: sid,
address: address,
locationId:locationId
};
$scope.leadsForMap.push($scope.location);
$scope.markers.push(marker);
}
在html中,我试图循环$scope.leadsForMap
数组并在点击后显示特定的详细信息
HTML
<div data-ng-repeat="location in leadsForMap track by $index" data-ng-
if="leadsForMap.length>0">
{{$index+1}} {{location.name}} - {{location.sid}},{{location.address}}
</div>
但即使$scope.leadsForMap
具有价值,这个div也根本不显示。谁能说出错在哪里?
答案 0 :(得分:2)
您需要使用
$scope.$apply()
当事件超出角度范围时,必须使用$scope.$apply
,而角度不会运行摘要周期,因为它超出了范围。
您需要运行摘要周期,该周期将由$ scope手动运行。$ apply()
google.maps.event.addListener(marker, 'click', function() {
$scope.location = {
name: name,
sid: sid,
address: address,
locationId:locationId
};
$scope.leadsForMap.push($scope.location);
$scope.markers.push(marker);
$scope.$apply();
}
&#13;