how to add style into component depending on body class in Angular 6

2,591 views
Skip to first unread message

Rakesh Roy

unread,
Sep 1, 2018, 2:41:46 PM9/1/18
to Angular and AngularJS discussion

app.component.ts

import { Component, Renderer2 } from '@angular/core';
import { Router, NavigationStart } from '@angular/router';

@Component({
  selector
: 'bb-root',
  templateUrl
: './app.component.html',
  styleUrls
: ['./app.component.scss']
})




export class AppComponent {
  title
= 'bb';
  previousUrl
: string;


  constructor
(private renderer: Renderer2, private router: Router) {
   
this.router.events
     
.subscribe((event) => {


       
if (event instanceof NavigationStart) {
         
if (this.previousUrl) {
           
this.renderer.removeClass(document.body, this.previousUrl);
         
}
         
const currentUrlSlug = event.url.slice(1);
         
if (currentUrlSlug) {
           
this.renderer.addClass(document.body, currentUrlSlug);
         
} else {
           
this.renderer.addClass(document.body, "home");
         
}
         
this.previousUrl = currentUrlSlug;
       
}
     
});


 
}




}

i added a body class depending on the route. and depending on the body class i ave to cane header color.

Class.png


So how can i add css and where 

Sander Elias

unread,
Sep 4, 2018, 1:02:18 AM9/4/18
to Angular and AngularJS discussion
Hi Rakesh,

There are s couple of ways to accomplish this. Disabling emulated view encapsulation is the easiest. Then just use normal CSS to address your issue.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages