place_changed
事件监听器不起作用。我有一个ID为street_number
的输入字段。
<input type="text" class="form-control" ng-model="newLocation.Street__c" placeholder="Street" id="street_number">
我想向该字段添加一个事件监听器。但是事件侦听器无法正常工作。下面是我的相关代码。
$scope.placeSearch = null;
$scope.autocompleteAssetLocation = null;
$scope.componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
$scope.initAutocomplete = function() {
$scope.autocompleteAssetLocation = new google.maps.places.Autocomplete(($element.find('#street_number')[0]), { types: ['geocode'] });
console.log($scope.autocompleteAssetLocation);
$scope.autocompleteAssetLocation.addListener('place_changed', function () { $scope.fillInAddress('autocompleteAssetLocation','Street__c', 'Street__c','text') });
};
$scope.onMapLoaded = function(){
$scope.mapLoaded = true;
$scope.$apply();
};
$scope.fillInAddress = function (autoCompleteName, searchModelName, outputModelName, inputType) {
console.log('DEBUG: Fill in address called at ' +Date.now());
var streetNumber = '';
var street = '';
var city = '';
var stateCode = '';
var zipCode = '';
var place = $scope[autoCompleteName].getPlace();
console.log(place);
if (!place) return;
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
switch (addressType) {
case 'street_number':
streetNumber = place.address_components[i]['short_name'];
break;
case 'route':
street = place.address_components[i]['long_name'];
break;
case 'locality':
city = place.address_components[i]['long_name'];
break;
case 'administrative_area_level_1':
stateCode = place.address_components[i]['short_name'];
break;
case 'postal_code':
zipCode = place.address_components[i]['short_name'];
break;
}
}
var fullAddress = null;
if(inputType==='textarea'){
fullAddress = (streetNumber ? streetNumber + ' ' : '')
+ (street ? street + '\n' : '')
+ (city ? city + ', ' : '')
+ (stateCode ? stateCode + ' ' : '')
+ (zipCode ? zipCode : '');
}
else{
fullAddress = (streetNumber ? streetNumber + ' ' : '')
+ (street ? street + ', ' : '')
+ (city ? city + ', ' : '')
+ (stateCode ? stateCode + ' ' : '')
+ (zipCode ? zipCode : '');
}
$scope.newLocation[outputModelName] = fullAddress;
$scope.$apply();
};
这是什么问题?