Home:ALL Converter>Getting DOM element properties in Angular

Getting DOM element properties in Angular

Ask Time:2018-01-11T04:46:18         Author:Sam Herrmann

Json Formatter

It is my understanding that it is bad practice in Angular to interact with DOM elements directly and that Renderer2 should be used instead. I have noticed however that Renderer2 is full of setter methods (setStyle, setProperty, setValue, etc..), but there are no getter methods. How should one obtain DOM element properties without interacting with the element directly, i.e elementRef.nativeElement.<propertyOfInterest>?

Example:

  • Get scrollTop value of Element: elementRef.nativeElement.scrollTop
  • Set scrollTop value of Element: renderer.setProperty(elementRef.nativeElement, 'scrollTop', myValue)

If this is the only way to get element property values, then why does that not defeat the purpose of Renderer2?

Reference Material:

Author:Sam Herrmann,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48195789/getting-dom-element-properties-in-angular
yy