I have diffirent classes for login page and other pages in application so after user logged in I need to change class of body element. Here how I am trying to accomplish thi
You can also use Renderer2 to modify the body class.
renderer.addClass(document.body, "myClass");
or
renderer.removeClass(document.body, "myClass");
One way is to use make the <body>
tag the app element by using body
as selector and use host-binding to update the app elements classes.
@Component({
selector: 'body',
host: {'[class.someClass]':'someField'}
})
export class AppComponent 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>`
}
}
Bindings outside <app-root>
are not supported.
What you can do is to use selector: 'body'
in you AppComponent
and
@HostBinding('class.dashboard')
dashboardClass = false;
@HostBinding('class.site-navbar-small')
siteNavbarSmallClass = false;
...
and then set the properties to true
to get the classes added.
or just
document.body.classList.add('dashboard');