Reputation: 1410
I have an input field with Google places autocomplete. Everything works as expected, but the event listener is not responding once a selection is made from the autocomplete dropdown. Shouldn't that be the expected behavior? Here is my code.
let input: any = this.geocodeTest.nativeElement;
var searchBox = new google.maps.places.Autocomplete(input);
searchBox.addListener("places_changed", () => {
this._ngZone.run(() => {
console.log('listening');
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
})
});
The code currently lives within the ngOnit()
Upvotes: 2
Views: 358
Reputation: 38171
As you mentioned you are running these code in ngOnInit
, and this is the reason because DOM elements are only avalible after AfterViewInit
lifehook.
you have to move your code to ngAfterViewInit
.
Upvotes: 1