![](/img/trans.png)
[英]AngularJS - ngSwitch and ngClick not working in ngRepeat
[英]AngularJs, ngRepeat not working
我正在使用AngularJs。 我正在顯示帶有標記的Google地圖。 單擊標記后,我將顯示單擊的標記詳細信息。
這是我的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也不顯示。 誰能告訴我哪里出了問題?
您需要使用
$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(); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.