2013-02-22 9 views
6

मेरे पास इसके लिए एक पहेली है, लेकिन मूल रूप से यह एक टेक्स्टबॉक्स में इनपुट करने वाले पते को भू-एन्कोडिंग कर रहा है। पता दर्ज करने के बाद और 'एंटर' दबाया जाता है, तो डोम तुरंत अपडेट नहीं होता है, लेकिन टेक्स्टबॉक्स में एक और परिवर्तन की प्रतीक्षा करता है। जमा करने के बाद तालिका को अपडेट करने के लिए मैं इसे कैसे प्राप्त करूं? मैं कोणीय के लिए बहुत नया हूं, लेकिन मैं सीख रहा हूं। मुझे यह दिलचस्प लगता है, लेकिन मुझे अलग-अलग सोचना सीखना है।कोणीय जेएस अपेक्षित होने पर डोम को अपडेट नहीं कर रहा है

यहाँ बेला है और मेरी 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; 
    }) 
    } 
} 

उत्तर

18

आपकी समस्या geocode समारोह AngularJS के बाहर अतुल्यकालिक और इसलिए अद्यतन चक्र को पचाने है। आप $scope.$apply के लिए एक कॉल में अपने कॉलबैक समारोह लपेटकर करके इसे ठीक कर सकते हैं, जिसकी मदद से AngularJS एक डाइजेस्ट चलाने के लिए जानते हैं, क्योंकि सामान बदल गया है:

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

धन्यवाद, मैं सिर्फ तुम क्या जरूरत है। कुछ नया सीख लिया। – bjo

+0

हाय, मुझे बिल्कुल वही समस्या है, लेकिन मैं इसे निर्देश और $ दायरे के अंदर करने की कोशिश कर रहा हूं। $ आवेदन मेरे लिए काम नहीं कर रहा है। मुझे नहीं पता कि मुझे कुछ याद आ रहा है या नहीं। कृपया मदद करे। – Rober

+0

@ रॉबर्ट क्या आप प्लंकर पोस्ट कर सकते हैं? –

संबंधित मुद्दे