how to change class of body in angular2&typescript project

前端 未结 3 341
温柔的废话
温柔的废话 2021-01-19 22:55

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

3条回答
  •  时光说笑
    2021-01-19 23:18

    One way is to use make the 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 ``
      }
    }
    

提交回复
热议问题