2013-09-11 99 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

感謝它的幫助。但實際的方法是。$適用於美元符號! –