K
K
Konstantin2021-03-04 23:12:48
Angular
Konstantin, 2021-03-04 23:12:48

Cannot read property 'nativeElement' in ngAfterContentInit, how to get reference to DOM element?

The code:

@ViewChild('container', { static: true }) container: ElementRef;


ngAfterContentInit() {
        try {
       
            this.mapLoaded = this.mapLibrary.ready;
        } catch (e) {
            console.error(e);
        }
    }


I get an error:
TypeError: Cannot read property 'nativeElement' of undefined


Sample:
<app-map-wrapper>
      <div id="map" #container></div>
    </app-map-wrapper>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question