How to get elementRef of an Ionic control like a Content, a toolbar or a Card?

I just upgraded to V4 and I am loving it. I still have a problem with trying to get elementRef of any ionic control like a card or a content.


	@ViewChild(Toolbar) toolbar: Toolbar;
	headerToolbar
	doSomthing(){
	this.headerToolbar = (this.toolbar as any).elementRef.nativeElement
		.getElementsByClassName('toolbar-background')[0]
	}

I always get the value of elementRef as undefined.

As of beta.6:

If you don’t want the nativeElement but rather like to have the Ionic/Stencil object/component, do as you displayed

 @ViewChild(Toolbar) toolbar: Toolbar;

if you do want the ElementRef respectively the nativeElement do as following (the read attribute is the key)

<ion-toolbar #myToolbar></ion-toolbar>

@ViewChild('myToolbar', {read: ElementRef}) toolbar: ElementRef;
4 Likes

Thanks, I had to use two different values, one for the elementRef and other for the Ionic control.

2 Likes