我正在尝试使用角度js使谷歌地方自动完成工作。这是jsfiddle - 在'place_change'事件之后,模型没有得到更新。它正在根据输入的变化进行更新。
下面是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 :(得分:5)
当place_change
事件被触发时,您需要告诉角度,因此它知道何时更新DOM。你可以致电$scope.$apply()
来做到这一点。 e.g:
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();
});