Search
Close this search box.

How to use Renderer2 in Angular JS

Introduction

Angular (commonly referred to as “Angular 2+” or “Angular v2 and above”) is a TypeScript-based open-source web application framework led by the Angular Team at Google and by a community of individuals and corporations. Angular is a complete rewrite from the same team that built AngularJS.

The Renderer2 class is an abstraction provided by Angular in the form of a service that allows manipulating elements of your app without having to touch the DOM directly. This is the recommended approach because it then makes it easier to develop apps that can be rendered in environments that don’t have DOM access, like on the server, in a web worker, or on native mobile.

Basic Usage

You’ll often use Renderer2 in custom directives because of how Angular directives are the logical building block for modifying elements. Here’s a simple example that uses Renderer2’s addClass method to add the wild class to elements that have the directive go-wild.directive.ts:

import { Directive, Renderer2, ElementRef, OnInit } from '@angular/core';

@Directive({
selector: '[appGoWild]'
})
export class GoWildDirective implements OnInit {
constructor(private renderer: Renderer2, private el: ElementRef) {}

Notice how we also use ElementRef to get access to the underlining native element that our directive is attached-to.

And now, you can add the directive to elements in a template and the wild class will be added when rendered:

Hello World!

You can see that overall the use of Renderer2 is not more complicated than manipulating the DOM directly. Let’s now go over some of the most useful methods:

createElement / appendChild / createText

Create new DOM elements and append them inside other elements. In this example, we create a new div and we create a text node. We then place the text node inside our new div and finally our div is added to the element referenced by our directive:

constructor(private renderer: Renderer2, private el: ElementRef) {}

ngOnInit() {
const div = this.renderer.createElement('div');
const text = this.renderer.createText('Hello world!');

this.renderer.appendChild(div, text);
this.renderer.appendChild(this.el.nativeElement, div);
}

Our template, once rendered, will look like this, given that we applied the directive on an article element:

Hello world!

setAttribute / removeAttribute

Use setAttribute or removeAttribute to do just that, set or remove an attribute:

constructor(private renderer: Renderer2, private el: ElementRef) {}

ngOnInit() {
this.renderer.setAttribute(this.el.nativeElement, 'aria-hidden', 'true');
}

addClass / removeClass

We’ve covered addClass in our above example. As for removeClass, simply provide the element reference and the name of the class to remove:

constructor(private renderer: Renderer2, private el: ElementRef) {}

ngOnInit() {
this.renderer.removeClass(this.el.nativeElement, 'wild');
}

setStyle / removeStyle

Use setStyle to add inline styles using Renderer2:

constructor(private renderer: Renderer2, private el: ElementRef) {}

ngOnInit() {
this.renderer.setStyle(
this.el.nativeElement,
'border-left',
'2px dashed olive'
);
}

…and removeStyle to remove it:

constructor(private renderer: Renderer2, private el: ElementRef) {}

ngOnInit() {
this.renderer.removeStyle(this.el.nativeElement, 'border-left');
}

setProperty

With the following example, you can set the alt property on an image element:

constructor(private renderer: Renderer2, private el: ElementRef) {}

ngOnInit() {
this.renderer.setProperty(this.el.nativeElement, 'alt', 'Cute alligator');
}

…or set the value of an input field:

// ...

ngOnInit() {
this.renderer.setProperty(this.el.nativeElement, 'value', 'Cute alligator');
}

Tips and Tricks

In this tutorial, you have successfully learned how to use Renderer2 on Angular. If you want to learn more about git checkout these documentations:
https://angular.io/docs

If you want a VPS server, get started now with VPSie and get one month for free.

This tutorial is to help you based on this original tutorial https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes please use our tutorial for notes and tips.

VPSie Cloud service

Fast and Secure Cloud VPS Service

Try FREE
For a month

The First 1 orders gets free discount today! Try Sign up on VPSie to get a chance to get the discount.