1
我得到了地址自動完成指令並獲取地點信息。此外,我添加了獲取城市ID和代碼在我以前的項目中工作的代碼,但現在無法工作(代碼中的數據[0] .place_id具有正確的值,但scope.form.object.localityId爲空功能PS scope.form.object之外。範圍變量沒有設置
...在指令和其他變量的父控制器聲明的填充正確
.directive('shAddressPredict', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, location) {
var options = {
types: ['address'],
};
scope.gPlace = new google.maps.places.Autocomplete(element[0], options);
google.maps.event.addListener(scope.gPlace, 'place_changed', function() {
var place = scope.gPlace.getPlace();
scope.form.object.fullAddress = place.name;
scope.form.object.placeId = place.place_id;
scope.form.object.locality = '';
scope.form.object.localityId = '';
scope.form.object.sublocality_level_1 = '';
scope.form.object.country = '';
var city = '';
angular.forEach(place.address_components, function(data) {
scope.form.object[data.types[0]] = data.long_name;
if(data.types[0] === 'locality') city += data.long_name + ', ';
if(data.types[0] === 'administrative_area_level_1') city += data.short_name + ', ';
if(data.types[0] === 'country') city += data.long_name;
});
// Geting city id
var service = new google.maps.places.AutocompleteService();
service.getPlacePredictions({
input: city,
types: ['(cities)']
}, function(data){
scope.form.object.localityId = data[0].place_id;
});
scope.$apply();
});
}
};
});
嗯...是的,對不對)Thx – pavolve