gpt4 book ai didi

angularjs - Google 使用 Angular JS 放置自动完成功能

转载 作者:行者123 更新时间:2023-12-02 22:36:55 25 4
gpt4 key购买 nike

我正在尝试让谷歌地点自动完成功能与 Angular 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;
});
});

最佳答案

您需要在 place_change 事件被触发时告诉 Angular,以便它知道何时更新 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();
});

关于angularjs - Google 使用 Angular JS 放置自动完成功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24636711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com