How to give class to a host element in Angular 2 with @hostbinding

后端 未结 1 574
有刺的猬
有刺的猬 2021-02-20 07:19

I want to give a class to the host element of my component so until now I used the host property like this:

@Component({
  selector: \'left-bar\',
          


        
1条回答
  •  遇见更好的自我
    2021-02-20 07:44

    The Angular2 style guide says to prefer @HostBinding, but this doesn't make host: {...} a bad thing.

    You can use

    @Component({
      selector: 'left-bar',
      templateUrl: 'app/left-bar/left-bar.component.html',
      styleUrls: ['app/left-bar/left-bar.component.css']
    })
    export class LeftBarComponent implements OnInit {
      @HostBinding('class.left-bar') leftBarClass = true;
      // or @HostBinding('class') leftBarClass = 'left-bar';
    
      ngOnInit() {
      }
    
    }
    

    0 讨论(0)
提交回复
热议问题