Javascript Google将带有Dart的API放在index.html之外
我正在尝试让Google Places地址自动完成在Dart组件中的工作 当我复制示例并将代码放在index.html文件中时,它会工作;当我尝试将部分或全部移动到组件中时,它不会工作。是否有我需要解决的范围问题?如果需要,我可以发布回购,但基本上,如果我将Javascript Google将带有Dart的API放在index.html之外,javascript,dart,angular-dart,Javascript,Dart,Angular Dart,我正在尝试让Google Places地址自动完成在Dart组件中的工作 当我复制示例并将代码放在index.html文件中时,它会工作;当我尝试将部分或全部移动到组件中时,它不会工作。是否有我需要解决的范围问题?如果需要,我可以发布回购,但基本上,如果我将input id=“autocomplete”移到索引文件之外,它就不起作用了,我会得到“js?key=xxxx&libraries=places&callback=initAutocomplete:42 InvalidValueError:
input id=“autocomplete”
移到索引文件之外,它就不起作用了,我会得到“js?key=xxxx&libraries=places&callback=initAutocomplete:42 InvalidValueError:不是HTMLInputElement的实例”
我怎样才能让它在组件中工作
下面的代码正在运行,但是如果我尝试将任何HTML移动到组件中,代码就会中断。如何将HTML移动到组件/模板中
<!DOCTYPE html>
<html>
<head>
<title>google_address</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<link rel="icon" type="image/png" href="favicon.png">
<script type="application/javascript">
let autocomplete;
const componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
</script>
<script type="application/javascript">
function initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
{types: ['geocode']});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);
}
</script>
<script type="application/javascript">
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
var event = new CustomEvent('address', {detail:place});
document.dispatchEvent(event);
}
</script>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxx&libraries=places&callback=initAutocomplete"
async defer></script>
<script defer src="main.dart.js"></script>
</head>
<body>
<div id="locationField">
<input id="autocomplete" placeholder="Enter your address" type="text">
</div>
<table id="address">
<tr>
<td class="label">Street address</td>
<td class="slimField"><input class="field" id="street_number"
disabled="true"></td>
<td class="wideField" colspan="2"><input class="field" id="route" disabled="true"></td>
</tr>
<tr>
<td class="label">City</td>
<!-- Note: Selection of address components in this example is typical.
You may need to adjust it for the locations relevant to your app. See
https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
-->
<td class="wideField" colspan="3"><input class="field" id="locality" disabled="true"></td>
</tr>
<tr>
<td class="label">State</td>
<td class="slimField"><input class="field" id="administrative_area_level_1" disabled="true"></td>
<td class="label">Zip code</td>
<td class="wideField"><input class="field" id="postal_code"
disabled="true"></td>
</tr>
<tr>
<td class="label">Country</td>
<td class="wideField" colspan="3"><input class="field"
id="country" disabled="true"></td>
</tr>
</table>
<my-app>Loading...</my-app>
</body>
</html>
谷歌地址
让自动完成;
常量组件形式={
街道编号:“短名称”,
路线:'long_name',
地点:'long_name',
行政区域级别1:“短名称”,
国家:'long_name',
邮政编码:“短名称”
};
函数initAutocomplete(){
//创建自动完成对象,将搜索限制为地理位置
//位置类型。
autocomplete=new google.maps.places.autocomplete(
/**@type{!HTMLInputElement}*/(document.getElementById('autocomplete'),
{类型:['geocode']});
//当用户从下拉列表中选择地址时,填充该地址
//表单中的字段。
autocomplete.addListener('place\u changed',fillInAddress);
}
函数fillInAddress(){
//从自动完成对象获取位置详细信息。
var place=autocomplete.getPlace();
for(componentForm中的var组件){
document.getElementById(组件).value='';
document.getElementById(组件).disabled=false;
}
//从place details中获取地址的每个组件
//并在表单上填写相应的字段。
对于(变量i=0;i
您所指的“示例”是什么?这里的示例:您能将Dart代码添加到您的问题中吗?(听起来好像你把代码翻译成了Dart)添加了代码,谢谢!这似乎只是JavaScript。我不明白这和Dart有什么关系。有一个Dart脚本标签,但我不知道这个问题与此有什么关系。