简体   繁体   English

AngularJs,ngRepeat无法正常工作

[英]AngularJs, ngRepeat not working

I am using AngularJs. 我正在使用AngularJs。 I am displaying google maps with markers. 我正在显示带有标记的Google地图。 After clicking marker I am displaying clicked marker details. 单击标记后,我将显示单击的标记详细信息。

Here is my Controller.js 这是我的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);
   }

In html I am trying to loop $scope.leadsForMap array and display particular details after click like this 在HTML中,我试图循环$scope.leadsForMap数组,并在单击后显示特定详细信息,如下所示

html 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>   

But this div is not at all displaying even if $scope.leadsForMap has value. 但是,即使$scope.leadsForMap具有值,此div也不显示。 Can anyone tell where it is wrong? 谁能告诉我哪里出了问题?

You need to use 您需要使用

$scope.$apply()

One has to use $scope.$apply when event is out of scope of angular and angular will not run digest cycle as it is out of scope. 当事件超出角度范围时,必须使用$scope.$apply ,并且角度超出范围时,角度不会运行摘要循环。

You need to run the digest cycle which is going to run manually by $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(); } 

Check When to use $scope.$apply() 检查何时使用$ scope。$ apply()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM