AngularJs,ngRepeat无效

时间:2018-04-25 09:59:18

标签: javascript html angularjs

我正在使用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也根本不显示。谁能说出错在哪里?

1 个答案:

答案 0 :(得分:2)

您需要使用

$scope.$apply()

当事件超出角度范围时,必须使用$scope.$apply,而角度不会运行摘要周期,因为它超出了范围。

您需要运行摘要周期,该周期将由$ scope手动运行。$ apply()

&#13;
&#13;
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;
&#13;
&#13;

检查When to use $scope.$apply()