I am building a simple Google Places Autocomplete Angular2 directive but the problem is that cannot get any prediction (the response is always empty?!)...
As a proof of concept I created the simplest possible code snippet as a reference:
<!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>
The above code works - I can get predictions after clicking Go button.
Now, Angular2 scenario:
My _Layout.cshtml file has the following tag in head section:
<script src="https://maps.googleapis.com/maps/api/js?key=[MY_API_KEY]&libraries=places" async defer></script>
My directive:
import { Directive, ElementRef, Input } from '@angular/core'; declare var google: any; @Directive({ selector: '[googleplaces]' }) export class GooglePlacesDirective { autocomplete: any; constructor(private el: ElementRef) { } ngAfterContentInit() { this.autocomplete = new google.maps.places.Autocomplete( (this.el.nativeElement), { types: ['geocode', 'cities'] }); } }
And simple Angular component:
<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>
The Scenario 2 (angular) doesn't work. The facts:
- autocomplete is initialized (it has all expected properties/methods, placeholder is "Enter a location", etc...)
- autocomplete doesn't return any prediction for typed search string (it returns only "/**/xdc._le3zv3 && xdc._le3zv3( [4] )")
Also, Google API Console says everything is as it should be?! Here is the screenshot:
What can be in question here? Thanks...