6

I read some answers already here, such as this, but my problem is slightly different. I want to get component as a DOM element so that I can add/remove some CSS classes on it.

My HTML template contains component:

<sidenav-menu #sidenav class="app__sidenav"></sidenav-menu>

And my class contains a reference to it:

@ViewChild('sidenav') sidenav: ElementRef;

But when I log this.sidenav in one of my functions, I see that it is a representation of a class of Sidenav component, not its DOM representation. Why is that happening?

1
  • see also this answer for reference what can be passed to {read:...} parameter Commented Aug 18, 2017 at 17:14

2 Answers 2

8

You can specify what you want using the read parameter:

@ViewChild('sidenav', {read: ElementRef}) sidenav: ElementRef;

This way you get the ElementRef and can use this.sidenav.nativeElement like mentioned by @joshrathke

or (for other use cases)

@ViewChild('sidenav', {read: ViewContainerRef}) sidenav: ElementRef;

My answer to the question you liked to also mentions this read parameter https://stackoverflow.com/a/35209681/217408

4
  • @TheOpti glad to hear :-) Commented Aug 18, 2017 at 15:50
  • Wouldn't a more "Angular-y" way to do this be using the ngClass directive?
    – DeborahK
    Commented Aug 18, 2017 at 20:46
  • @DeborahK I looked at the question again but I can't find information about what the actual intention was. You might be right. I just pointed out what struck me what can't work and how to fix it. Commented Aug 18, 2017 at 20:49
  • 1
    @GünterZöchbauer Thanks for the info. Wasn't sure if I was just missing something. Yea ... sometimes when there is not enough info its hard to drill down to what they are really trying to do and suggest alternatives vs just answering what they are asking..
    – DeborahK
    Commented Aug 18, 2017 at 20:52
3

Edit

In order for this to work, you need to implement @Günter Zöchbauer suggestion for extracting the component using ViewChild with read.

Original Answer

You need to use the nativeElement property of the ElementRef object. You'll find your DOM Representation there. Its also what exposes the DOM element and allows you to call traditional methods on it as if it was a DOM element in vanilla javascript or jQuery.

console.log(this.sidenav.nativeElement);
4
  • 1
    That doesn't work, if the element is component, the component instance is injected. Adding :ElementRef has no effect. Commented Aug 18, 2017 at 15:45
  • 1
    Agree, that does not work - I do not have such property when I log this.sidenav
    – TheOpti
    Commented Aug 18, 2017 at 15:47
  • 1
    Ahh, You're right. I missed that completely.Thank you for the clarification @GünterZöchbauer.
    – joshrathke
    Commented Aug 18, 2017 at 15:48
  • 1
    I went ahead and edited the post to refer to your post @GünterZöchbauer, thanks for the correction.
    – joshrathke
    Commented Aug 18, 2017 at 15:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.