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;
}
}