Either on PageLoaded or on ngAfterViewInit, the problem is that getElementById is returning a TextIput (from an ion-input) not an HTMLInputElement (regular input) as Google API is expecting.
I have no idea how to either cast it, or get the inside the .
Using @joshmorony’s solution should work.
The ion-input element in a template creates more than just an input element when it is rendered, which explains why people are getting the “not an instance of HTMLInputElement” error.
I’m wondering though, if there are any other ways of getting a component’s elements from the angular framework rather than searching the DOM.
I’m worried about more complex pages that use components multiple times, potentially resulting in duplication of an ID.
It is strange because for me this.myElement is a TextInput object, and this.myElement.nativeElement is undefined. I am paying attention to your ngAfterViewInit as well.
Here is how i got it working
import { Component, ViewChild, NgZone, ElementRef } from ‘@angular/core’;
export class MapPage { @ViewChild(“search”, { read: ElementRef })
public searchElementRef;
constructor(){}
autocomplete() {
… blabla
let inputField = this.searchElementRef.nativeElement.getElementsByTagName('input')[0];
let autocomplete = new google.maps.places.Autocomplete( inputField, {
types: ["address"]
});