Renderer和ElementRef在角度2中的区别 [英] Difference between Renderer and ElementRef in angular 2
问题描述
Renderer
和ElementRef
有什么区别?在Angular中,两者都用于DOM操作.我目前正在单独使用ElementRef
来编写Angular 2指令.如果我获得有关Renderer
的更多信息,则可以在以后的指令中使用它.
What is the difference between Renderer
and ElementRef
? In Angular both are used for DOM Manipulation. I am currently using ElementRef
alone for writing Angular 2 directives. If I get more info about Renderer
, I can use that in my future directives.
推荐答案
Renderer
是一个类,是DOM上的部分抽象.
使用Renderer
来操作DOM不会破坏服务器端渲染或Web Workers(在这些位置上直接访问DOM会中断).
The Renderer
is a class that is a partial abstraction over the DOM.
Using the Renderer
for manipulating the DOM doesn't break server-side rendering or Web Workers (where direct access to the DOM would break).
ElementRef
是一个类,可以保存对DOM元素的引用.
再次,这是在浏览器DOM实际不可用的环境中不会中断的抽象.
ElementRef
is a class that can hold a reference to a DOM element.
This is again an abstraction to not break in environments where the browsers DOM isn't actually available.
如果将ElementRef
注入到组件,则注入的实例是对当前组件的宿主元素的引用.
If ElementRef
is injected to a component, the injected instance is a reference to the host element of the current component.
还有其他获取ElementRef
实例的方法,例如@ViewChild()
,@ViewChildren()
,@ContentChild()
,@ContentChildren()
.在这种情况下,ElementRef
是对模板或子级中匹配元素的引用.
There are other ways to acquire an ElementRef
instance like @ViewChild()
, @ViewChildren()
, @ContentChild()
, @ContentChildren()
. In this case ElementRef
is a reference to the matching element(s) in the template or children.
Renderer
和ElementRef
并非这个或那个",而是必须一起使用以获得完整的平台抽象.
Renderer
and ElementRef
are not "either this or that", but instead they have to be used together to get full platform abstraction.
Renderer
作用于DOM,而ElementRef
是对Renderer
作用于DOM中的元素的引用.
Renderer
acts on the DOM and ElementRef
is a reference to an element in the DOM the Renderer
acts on.
这篇关于Renderer和ElementRef在角度2中的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!