How to change body class using typescript class (angular2)

How to change body class via root component?

@Component({ selector: "app", directives: [ROUTER_DIRECTIVES], template: ` <section class="header"> <h1>App</h1> <router-outlet></router-outlet> ` }) 
+18
angular styles
Jan 06 '16 at 15:21
source share
6 answers

One way that does not depend on the direct manipulation of the DOM is to make the <body> tag an app element using body as a selector and use the host binding to update the application element classes.

 @Component({ selector: 'body', host: {'[class.someClass]':'someField'} }) export class AppElement implements AfterViewInit { someField: bool = false; // alternatively to the host parameter in `@Component` // @HostBinding('class.someClass') someField: bool = false; ngAfterViewInit() { someField = true; // set class `someClass` on `<body>` } } 
+19
Jan 06 '16 at 15:41
source share
— -

Here you can simply use native JavaScript in the Angular2 component to change the class of the <body> : -

 let body = document.getElementsByTagName('body')[0]; body.classList.remove("className"); //remove the class body.classList.add("className"); //add the class 
+13
Oct 19 '16 at 9:15
source share

We are looking for the best solution, here is my current solution:

 import { Component, OnInit, OnDestroy, ViewEncapsulation } from '@angular/core'; @Component({ selector: 'signup', templateUrl: './signup.component.html', styleUrls: ['./signup.component.css',], // Where my custom CSS styles for body element declared encapsulation: ViewEncapsulation.None, // That will not encapsulate my CSS styles (layout-full, page-signup) from signup.component.css inside component }) export class SignupComponent implements OnInit, OnDestroy{ bodyClasses:string = "layout-full page-signup"; ngOnInit(): void { $('body').addClass(this.bodyClasses); } ngOnDestroy() { $('body').removeClass(this.bodyClasses); } } 
+4
Nov 05 '16 at 13:32
source share

i reinstall it using routing - for example, -add to the root-app component this code:

  this.activeRouter.events.subscribe( data => { this.className = data.url.split('/').join(' ').trim(); this.changeBodyClass(); }) 

and body change:

 changeBodyClass(){ if(this.el.nativeElement.parentElement.nodeName === 'BODY'){ this.el.nativeElement.parentElement.className = this.className ? this.className + '-page' : 'home-page'; } 

you need to enter into the constructor:

 constructor(private activeRouter: Router, private el: ElementRef) 
+2
Mar 02 '17 at 13:45
source share

Use the code below.

  ngOnInit() { let body = document.getElementsByTagName('body')[0]; body.classList.add('body-landing'); } ngOnDestroy() { let body = document.getElementsByTagName('body')[0]; body.classList.remove("body-landing"); } 
+2
Sep 19 '17 at 11:39 on
source share

In case someone needs to add and remove a class from the body only with the active specific component, this can be done, as shown below. In my specific case, I wanted to add the landing page class only when the user lands on the Home Page (View) and removes this class when the user navigates to other views:

 import {Component, OnInit, OnDestroy} from '@angular/core'; export class HomeComponent implements OnInit { constructor() {} //Add the class to body tag when the View is initialized ngOnInit() { let body = document.getElementsByTagName('body')[0]; body.classList.add("landing-page"); } //Remove the class from body tag when the View is destroyed ngOnDestroy() { let body = document.getElementsByTagName('body')[0]; body.classList.remove("landing-page"); } } 
+1
May 2 '17 at 17:48
source share



All Articles