2013-09-11 97 views
0

我在试图修改从GMaps事件的角模型时有点麻烦,继承人是代码:AngularJS范围与GMaps

function CtrlGMap($scope) { 

var mapOptions = { 
    center: new google.maps.LatLng(-54.798112, -68.303375), 
    zoom: 11, 
    //disableDefaultUI: true, 
    mapTypeId: google.maps.MapTypeId.SATELLITE 
}; 

map = new google.maps.Map(document.getElementById('mapCanvas'), mapOptions); 

google.maps.event.addListener(map, "click", function(e) { 
    lat = e.latLng.lat(); 
    lng = e.latLng.lng(); 

    $scope.lat = lat; 
)}; 

HTML:

<body ng-controller="CtrlGMap"> 

<div id="mapCanvas"></div> 

<form role="form" style="width: 30%; margin:0 auto;"> 

    <div class="form-inline"> 
     <div class="form-group"> 
      <label class="sr-only" for="lat">Latitud</label> 
      <div class="input-group"> 
       <span class="input-group-addon">Lat</span> 
       <input type="text" class="form-control" id="lat" ng-model="lat" disabled> 
      </div> 
     </div> 
    </div> 
</form> 

的事情是,我没有从GMap事件内部访问$ scope ...为什么是这样?

+0

可能有助于如果你能对你的意思是“我已经从GMAP事件里面$范围无法访问”什么细说。 –

回答

0

我想你需要调用$ scope.apply(),如果你正在改变angularjs领域以外的范围。

所以这样的:

google.maps.event.addListener(map, "click", function(e) { 
    lat = e.latLng.lat(); 
    lng = e.latLng.lng(); 

    $scope.lat = lat; 
    // Let angularjs know. 
    $scope.$apply(); 
)}; 
+0

感谢它的帮助。但实际的方法是。$适用于美元符号! –