Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change class of body in angular2&typescript project

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 this..

index.html

<body [ngClass]="{
  'dashboard site-navbar-small' :isAuthenticated,
  'login-form login-form-second page-login-second' :!isAuthenticated
}">
  <app-root>Loading...</app-root>

login.component.ts

export class LoginComponent {
  @HostBinding('class.login-form.login-form-second.page-login-second')
  siteNavbarSmallClass = false;
  constructor(private auth:Auth){
    this.siteNavbarSmallClass=this.auth.authenticated();
  }
}

app.component.ts

 export class AppComponent {
  @HostBinding('class.dashboard.site-navbar-small')
  dashboardClass = false;
  constructor(private auth:Auth){
      this.dashboardClass=this.auth.authenticated();
  }
}

I am trying to bind ngClass directive to isAuthenticated field.. but I doesnt affected. I heard we are not able to change body element via ts, how can I handle it with anyway ?

like image 794
TyForHelpDude Avatar asked Dec 20 '16 18:12

TyForHelpDude


Video Answer


1 Answers

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');
like image 130
Günter Zöchbauer Avatar answered Oct 02 '22 17:10

Günter Zöchbauer