Renderer2

class

Extend this base class to implement custom rendering. By default, Angular renders a template into DOM. You can use custom rendering to intercept rendering calls, or to render to something other than DOM.

See more...

abstract class Renderer2 {
  abstract data: {...}
  destroyNode: ((node: any) => void) | null
  abstract destroy(): void
  abstract createElement(name: string, namespace?: string | null): any
  abstract createComment(value: string): any
  abstract createText(value: string): any
  abstract appendChild(parent: any, newChild: any): void
  abstract insertBefore(parent: any, newChild: any, refChild: any): void
  abstract removeChild(parent: any, oldChild: any): void
  abstract selectRootElement(selectorOrNode: string | any): any
  abstract parentNode(node: any): any
  abstract nextSibling(node: any): any
  abstract setAttribute(el: any, name: string, value: string, namespace?: string | null): void
  abstract removeAttribute(el: any, name: string, namespace?: string | null): void
  abstract addClass(el: any, name: string): void
  abstract removeClass(el: any, name: string): void
  abstract setStyle(el: any, style: string, value: any, flags?: RendererStyleFlags2): void
  abstract removeStyle(el: any, style: string, flags?: RendererStyleFlags2): void
  abstract setProperty(el: any, name: string, value: any): void
  abstract setValue(node: any, value: string): void
  abstract listen(target: 'window' | 'document' | 'body' | any, eventName: string, callback: (event: any) => boolean | void): () => void
}

Description

Create your custom renderer using RendererFactory2.

Use a custom renderer to bypass Angular's templating and make custom UI changes that can't be expressed declaratively. For example if you need to set a property or an attribute whose name is not statically known, use the setProperty() or setAttribute() method.

Properties

Property Description
abstract data: { [key: string]: any; } Read-only.

Use to store arbitrary developer-defined data on a renderer instance, as an object containing key-value pairs. This is useful for renderers that delegate to other renderers.

destroyNode: ((node: any) => void) | null

If null or undefined, the view engine won't call it. This is used as a performance optimization for production mode.

Methods

Implement this callback to destroy the renderer or the host element.

abstract destroy(): void

Parameters

There are no parameters.

Returns

void

Implement this callback to create an instance of the host element.

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

Parameters

name

An identifying name for the new element, unique within the namespace.

namespace

The namespace for the new element.

Optional. Default is undefined.

Returns

any: The new element.

Implement this callback to add a comment to the DOM of the host element.

abstract createComment(value: string): any

Parameters

value

The comment text.

Returns

any: The modified element.

Implement this callback to add text to the DOM of the host element.

abstract createText(value: string): any

Parameters

value

The text string.

Returns

any: The modified element.

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

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

Parameters

parent

The parent node.

newChild

The new child node.

Returns

void

Implement this callback to insert a child node at a given position in a parent node in the host element DOM.

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

Parameters

parent

The parent node.

newChild

The new child nodes.

refChild

The existing child node that should precede the new node.

Returns

void

Implement this callback to remove a child node from the host element's DOM.

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

Parameters

parent

The parent node.

oldChild

The child node to remove.

Returns

void

Implement this callback to prepare an element to be bootstrapped as a root element, and return the element instance.

abstract selectRootElement(selectorOrNode: string | any): any

Parameters

selectorOrNode

The DOM element.

Returns

any: The root element.

Implement this callback to get the parent of a given node in the host element's DOM.

abstract parentNode(node: any): any

Parameters

node

The child node to query.

Returns

any: 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.

Implement this callback to get the next sibling node of a given node in the host element's DOM.

abstract nextSibling(node: any): any

Parameters

node

Type: any.

Returns

any: 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.

Implement this callback to set an attribute value for an element in the DOM.

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

Parameters

el

The element.

name

The attribute name.

value

The new value.

namespace

The namespace.

Optional. Default is undefined.

Returns

void

Implement this callback to remove an attribute from an element in the DOM.

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

Parameters

el

The element.

name

The attribute name.

namespace

The namespace.

Optional. Default is undefined.

Returns

void

Implement this callback to add a class to an element in the DOM.

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

Parameters

el

The element.

name

The class name.

Returns

void

Implement this callback to remove a class from an element in the DOM.

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

Parameters

el

The element.

name

The class name.

Returns

void

Implement this callback to set a CSS style for an element in the DOM.

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

Parameters

el

The element.

style

The name of the style.

value

The new value.

flags

Flags for style variations. No flags are set by default.

Optional. Default is undefined.

Returns

void

Implement this callback to remove the value from a CSS style for an element in the DOM.

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

Parameters

el

The element.

style

The name of the style.

flags

Flags for style variations to remove, if set. ???

Optional. Default is undefined.

Returns

void

Implement this callback to set the value of a property of an element in the DOM.

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

Parameters

el

The element.

name

The property name.

value

The new value.

Returns

void

Implement this callback to set the value of a node in the host element.

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

Parameters

node

The node.

value

The new value.

Returns

void

Implement this callback to start an event listener.

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

Parameters

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.

Returns

() => void: An "unlisten" function for disposing of this handler.

© 2010–2019 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v6.angular.io/api/core/Renderer2