top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Whar is use for Renderer2 in Angular(2/4/5) ?

0 votes
387 views
Whar is use for Renderer2 in Angular(2/4/5) ?
posted Dec 31, 2017 by Parampreet Kaur

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The Renderer2 class is an abstraction provided by Angular in the form of a service that allows to manipulate 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.

Example:

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

  constructor(private renderer: Renderer2) {
  }

  ngOnInit() {
     if (document.body.classList.contains('sidenav-toggled')) {
      this.renderer.removeClass(document.body, 'sidenav-toggled');
      this.sideNavIcon = false;
    } else {
      this.renderer.addClass(document.body, 'sidenav-toggled');
      this.sideNavIcon = true;
    }
  }
answer Dec 31, 2017 by Chahat Sharma
...