2013-02-22 32 views
6

Bunun için bir keman var, ama temel olarak bir metin kutusuna giren bir adresi coğrafi olarak kodlayan bir şey yapıyor. Adres girildikten ve 'enter' basıldıktan sonra dom hemen güncellenmez, ancak metin kutusuna başka bir değişiklik yapılmasını bekler. Bir gönderimden hemen sonra tabloyu güncellemek için nasıl alabilirim? Angular'da çok yeniyim, ama öğreniyorum. İlginç buluyorum ama farklı düşünmeyi öğrenmeliyim.Açısal js beklendiği zaman dom güncellenmiyor

İşte keman ve benim controller.js

http://jsfiddle.net/fPBAD/

var myApp = angular.module('geo-encode', []); 

function FirstAppCtrl($scope, $http) { 
    $scope.locations = []; 
    $scope.text = ''; 
    $scope.nextId = 0; 

    var geo = new google.maps.Geocoder(); 

    $scope.add = function() { 
    if (this.text) { 

    geo.geocode(
     { address : this.text, 
      region: 'no' 
     }, function(results, status){ 
      var address = results[0].formatted_address; 
      var latitude = results[0].geometry.location.hb; 
      var longitude = results[0].geometry.location.ib; 

      $scope.locations.push({"name":address, id: $scope.nextId++,"coords":{"lat":latitude,"long":longitude}}); 
    }); 

     this.text = ''; 
    } 
    } 

    $scope.remove = function(index) { 
    $scope.locations = $scope.locations.filter(function(location){ 
     return location.id != index; 
    }) 
    } 
} 

cevap

18

Senin sorunun geocode fonksiyon angularjs dışında asenkron ve bu nedenle güncelleştirmeler döngüsünü sindirmek olmasıdır. Sen şeyler değiştiği için angularjs bir özetini çalıştırmak bildirir ki, $scope.$apply bir çağrı, geri arama işlevi sarma bu sorunu giderebilirsiniz:

geo.geocode(
    { address : this.text, 
    region: 'no' 
    }, function(results, status) { 
    $scope.$apply(function() { 
     var address = results[0].formatted_address; 
     var latitude = results[0].geometry.location.hb; 
     var longitude = results[0].geometry.location.ib; 

     $scope.locations.push({ 
     "name":address, id: $scope.nextId++, 
     "coords":{"lat":latitude,"long":longitude} 
     }); 
    }); 
}); 
+0

Teşekkür, ben sadece ne gerekli. Yeni bir şey öğrendim. – bjo

+0

Merhaba, Bende aynı sorun var, ama bunu bir direktif ve $ kapsam içinde yapmaya çalışıyorum. $ Başvuru benim için çalışmıyor. Bir şey özlediğimi bilmiyorum. Lütfen yardım et. – Rober

+0

@Rober Plunker gönderebilir misiniz? –

İlgili konular