【发布时间】:2017-07-09 13:50:51
【问题描述】:
我正在构建一个简单的 Google Places Autocomplete Angular2 指令,但问题是无法获得任何预测(响应始终为空?!)...
作为概念证明,我创建了最简单的代码 sn-p 作为参考:
<!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?key=[MY_API_KEY]&libraries=places" async defer></script>
</head>
<body>
<button type="button" onclick="go()">go</button>
<input id="autocomplete" type="text"></input>
<script>
var autocomplete = null;
function go() {
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')), {
types: ['geocode']
});
}
</script>
</body>
</html>
上面的代码有效——点击Go按钮后我可以得到预测。
现在,Angular2 场景:
我的 _Layout.cshtml 文件在 head 部分有以下标签:
<script src="https://maps.googleapis.com/maps/api/js?key=[MY_API_KEY]&libraries=places" async defer></script>
我的指令:
从'@angular/core'导入{指令,ElementRef,输入}; 声明 var google: any; @Directive({ 选择器:'[googleplaces]' }) 导出类 GooglePlacesDirective { 自动完成:任何; 构造函数(私有 el:ElementRef){ } ngAfterContentInit() { this.autocomplete = 新的 google.maps.places.Autocomplete( (this.el.nativeElement), { 类型:['geocode', 'cities'] }); } }还有简单的 Angular 组件:
<form [formGroup]="companyForm">
.
.
.
<div class="form-group">
<label for="Location">Location</label>
<input type="text"
class="form-control"
id="Location"
fromControlName="Location"
googleplaces>
</div>
</form>
场景 2(角度)不起作用。事实:
- 自动完成已初始化(它具有所有预期的属性/方法,占位符是“输入位置”等...)
- 自动完成不会为键入的搜索字符串返回任何预测(它只返回“/**/xdc._le3zv3 && xdc._le3zv3( [4] )” )
另外,谷歌 API 控制台说一切都是应该的?!这是截图:
这里有什么问题?谢谢...
【问题讨论】: