Renderer2

This is the documentation for Angular 6.
You can switch to the latest version Angular 9.

The Renderer class is a service that provides an abstraction for UI rendering manipulations. Using it is the recommended approach because it then makes it easier to develop apps that can be rendered in different environments.

Links & Tutorials

Methods

destroy()#

destroy(): void

Destroy the renderer or the host element.

createElement()#

createElement(name: string, namespace?: string | null): any

Create an instance of the host element.

  • name — an identifying name for the new element, unique within the namespace.
  • namespace - the namespace for the new element.

createComment()#

createComment(value: string): any

Add a comment to the DOM of the host element.

createText()#

createText(value: string): any

Add text to the DOM of the host element.

appendChild()#

appendChild(parent: any, newChild: any): void

Appends a child to a given parent node in the host element DOM.

insertBefore()#

insertBefore(parent: any, newChild: any, refChild: any): void

Insert a child node at a given position in a parent node in the host element DOM.

  • parent — the parent node.
  • newChild — the new child nodes.
  • refChild — the existing child node that should precede the new node.

removeChild()#

removeChild(parent: any, oldChild: any): void

Remove a child node from the host element's DOM.

selectRootElement()#

selectRootElement(selectorOrNode: string | any): any

Prepare an element to be bootstrapped as a root element, and return the element instance.

parentNode()#

parentNode(node: any): any

Get the parent of a given node in the host element's DOM.

Returns the parent node, or null if there is no parent. For WebWorkers, always returns true. This is because the check is synchronous, and the caller can't rely on checking for null.

nextSibling()#

nextSibling(node: any): any

Get the next sibling node of a given node in the host element's DOM.

Returns the sibling node, or null if there is no sibling. For WebWorkers, always returns a value. This is because the check is synchronous, and the caller can't rely on checking for null.

setAttribute()#

setAttribute(el: any, name: string, value: string, namespace?: string | null): void

Set an attribute value for an element in the DOM.

removeAttribute()#

removeAttribute(el: any, name: string, namespace?: string | null): void

Remove an attribute from an element in the DOM.

addClass()#

addClass(el: any, name: string): void

Add a class to an element in the DOM.

removeClass()#

removeClass(el: any, name: string): void

Remove a class from an element in the DOM.

setStyle()#

setStyle(el: any, style: string, value: any, flags?: RendererStyleFlags2): void

Set a CSS style for an element in the DOM.

removeStyle()#

removeStyle(el: any, style: string, flags?: RendererStyleFlags2): void

Remove the value from a CSS style for an element in the DOM.

setProperty()#

setProperty(el: any, name: string, value: any): void

Set the value of a property of an element in the DOM.

setValue()#

setValue(node: any, value: string): void

Set the value of a node in the host element.

listen()#

listen(target: 'window' | 'document' | 'body' | any, eventName: string, callback: (event: any) => boolean | void): () => void

Start an event listener.

  • target — The context in which to listen for events. Can be the entire window or document, the body of the document, or a specific DOM element.

  • eventName — The event to listen for.

  • callback — A handler function to invoke when the event occurs.