Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElementRef and @ViewChild, is not recommended?

Tags:

angular

I new in Angular and I try to understand:

The directive @ViewChild have any use without Elementref?

Elementref access to DOM direcly? or to Virtual DOM?

Why angular team say: "Permitting direct access to the DOM can make your application more vulnerable to XSS attacks." That mean Vanila JS is danger due the claim: JavaScript access DOM?

like image 387
Matanya Cohen Avatar asked Sep 06 '25 17:09

Matanya Cohen


1 Answers

ViewChild is a property decorator that helps you to inject a reference to a component, or to a DOM reference.
In your case, you're concern about the latter. ElementRef wraps a reference to the DOM element.

Angular provides tools for DOM manipulation - such as Renderer - with built-in sanitization mechanisms, if you use ElementRef to modify the DOM, you need to handle your own sanitization mechanism (because you bypass Angular DOM sanitization) in order to avoid XSS Attacks. If you don't modify the DOM, you're safe by design.

like image 147
Florian Avatar answered Sep 10 '25 09:09

Florian