2014-07-08 157 views
5

我想讓谷歌地方自動填充角js工作。這裏是jsfiddle - 模型在'place_change'事件後沒有得到更新。它正在更新輸入的更改。谷歌地方自動填充角js

下面是HTML代碼 - HTML

<body ng-app="mapApp"> 
    <div ng-controller="MapController"> 
     <input id="from" type="text" ng-model="user.from" placeholder="Type Address" class="ng-pristine ng-valid" autocomplete="off"> 
     <input type="hidden" ng-model="user.fromLat"> 
     <input type="hidden" ng-model="user.fromLng"> 
      <p>{{user.from}} <br> {{'Latitude : ' + user.fromLat + ', Longitutde : ' + user.fromLng}}</p> 
    </div> 
</body> 

Java腳本

var mapApp = angular.module('mapApp', []); 

mapApp.controller('MapController', function ($scope) { 
    $scope.user = {'from': '', 'fromLat': '', 'fromLng' : ''}; 
    var options = { 
     componentRestrictions: {country: "in"} 
    }; 
    var inputFrom = document.getElementById('from'); 
    var autocompleteFrom = new google.maps.places.Autocomplete(inputFrom, options); 
    google.maps.event.addListener(autocompleteFrom, 'place_changed', function() { 
     var place = autocompleteFrom.getPlace(); 
     $scope.user.fromLat = place.geometry.location.lat(); 
     $scope.user.fromLng = place.geometry.location.lng(); 
     $scope.user.from = place.formatted_address; 
    }); 
}); 
+0

您的jsfiddle鏈接似乎並不奏效 – rob

+0

更新的jsfiddle - http://jsfiddle.net/punchouty/cTD2a/6/ – Puja

+0

'VAR inputFrom =的document.getElementById( '從'); '反對控制器最佳實踐中的角度不操縱,不是嗎?我們怎麼能用一個指令來實現這個? – sgimeno

回答

5

您需要時place_change事件被炒魷魚,所以它知道何時更新DOM告訴角度。您可以致電$scope.$apply()。例如:

google.maps.event.addListener(autocompleteFrom, 'place_changed', function() { 
     var place = autocompleteFrom.getPlace(); 
     $scope.user.fromLat = place.geometry.location.lat(); 
     $scope.user.fromLng = place.geometry.location.lng(); 
     $scope.user.from = place.formatted_address; 
     $scope.$apply(); 
    }); 
+0

謝謝搶劫。有效 :) – Puja

相關問題