How to get reference to a dom element (angular2-beta.1)

571 views
Skip to first unread message

mat b.

unread,
Jan 29, 2016, 5:22:35 PM1/29/16
to AngularJS
I have a component with a template and want to get the reference to a child div.
Tried to use ElementRef.nativeElement.querySelector("#map") but this is not working // returns null
Need the reference to use it as target for a openlayers map (http://openlayers.org/en/v3.13.0/apidoc/ol.Map.html)

thanks for any help!

Günter Zöchbauer

unread,
Jan 30, 2016, 9:10:31 AM1/30/16
to AngularJS
elementRef.nativeElement.querySelector() should work

Alternatively you can add a template variable

    <div #someVar></div>

and then use 

    @ViewChild('someVar') myDiv;

and Angular assigns a reference to the div to myDiv (initialized after ngAfterViewInit()) is done)

mat b.

unread,
Jan 30, 2016, 3:00:57 PM1/30/16
to AngularJS
elementRef.nativeElement.querySelector() doesn't work...
maybe it's wrong to do it in the component constructor?

But the trick with ViewChild and ngAfterViewInit worked ...
thanks for help :)
Reply all
Reply to author
Forward
0 new messages