Renderer和ElementRef在角度2中的区别 [英] Difference between Renderer and ElementRef in angular 2

查看:170
本文介绍了Renderer和ElementRef在角度2中的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RendererElementRef有什么区别?在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.

RendererElementRef并非这个或那个",而是必须一起使用以获得完整的平台抽象.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆