How can I get css properties from a ElementRef element?
I've tried to use element.style but all the properties are empty.
The Renderer class has this.renderer.setElementStyle method but there is no this.renderer.getElementStyle method.
1 Answer
elementRef instance has a nativeElement property which is an element node itself and contains most of the properties one may need.
Another solution which is well supported by modern browsers is
window.getComputedStyle(elementRef.nativeElement)
6 Comments
Yaroslav Grishajev
Well most properties of Angular components are empty by default unless you set some. What property do you need?
Ben
I need the css properties that are set by css code. In my case I'm trying to find the display property but my question is more like a global question.
Yaroslav Grishajev
not the best solution I guess, but it works
window.getComputedStyle(elementRef.nativeElement)Ben
Thank you it works perfect! But I'm using this code in ngAfterViewChecked which is called a lot of times. Does
getComputedStyle use a lot of memory? And can you please edit you post so I can accept it as answer?Yaroslav Grishajev
Ok, edited. I can't give an answer on it's performance. This needs some research.
|