'Angular universal alternative to queryselector
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.
Solution 1:[1]
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
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Yunus Yerli |
