All the plugins which are currently supporting angular 2.0 don't seem to support Angular Universal. It shows errors due to the direct DOM
access. I want to alter the DOM
access code in the angular 2 way. Can anyone suggest the angular 2 way for window, document,queryselector()
, querySelectorAll()
, document.getElementsByTagname()
, getElementsByClassName()
, etc.
Asked
Active
Viewed 3,711 times
5

Kim Kern
- 54,283
- 17
- 197
- 195

Mohendran KM
- 51
- 1
- 3
-
Check out `@ViewChild` and `@HostBinding`, depending on what you want to do. – Dan Wilson Nov 22 '16 at 17:16
2 Answers
0
At first, direct DOM access is discouraged in Angular2. You should prefer using directives like *ngFor
and custom structural directives to update the DOM.
You can use one of the methods mentioned in angular 2 / typescript : get hold of an element in the template to get hold of an element and use what the Renderer provides to modify the DOM without breaking Universal.
For structural directives see also

Community
- 1
- 1

Günter Zöchbauer
- 623,577
- 216
- 2,003
- 1,567
0
Direct Dom access is discouraged in Angular. Also, you can see comments about using elementRef. elementRef is also discouraged. For elementRef, Angular teams says:"Use this API as the last resort when direct access to DOM is needed.".
You can use renderer2 library. Use this library with elementRef together.

Yunus Yerli
- 89
- 5
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 25 '22 at 14:15