在angular5项目中使用纯JavaScript(类似于getDocumentById
语法)是一种良好的编码习惯,或者我们应该使用ElementRef
、 TemplateRef
、 ContainerRef
来引用DOM
元素而不是ElementRef
JavaScript。
It's not encouraged to use the DOM element manipulation directly . This blog explains why it should be avoided
With Angular 6 last update by google they have started moving towards shadow dom and dom hierarchy tree it would help dev community to not worrying about actual dom elements manipulation
Use the framework, don't go direct to the DOM (which is what you mean by "plain JavaScript") unless you have no other choice. (Or don't use a framework at all.)
But note the warning in the ElementRef
docs :
Permitting direct access to the DOM can make your application more vulnerable to XSS attacks. Carefully review any use of
ElementRef
in your code. For more detail, see the Security Guide .
If you can avoid using ElementRef
(without going direct to the DOM), that's probably best.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.