Is it possible to add a dynamic class to host in Angular 2?

后端 未结 7 1996
眼角桃花
眼角桃花 2021-01-01 15:47

I know that in Angular2 I can add a class \'red\' to a component\'s selector element by doing this:

@Component({
    selector: \'selector-el\',
    host: {
          


        
7条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 15:58

    You can use something like that:

    @Directive({
      (...)
      host: {
        '[class.className]' : 'className', 
        '[class]' : 'classNames' 
      }
    }
    export class MyDirective {
      constructor() {
        this.className = true;
        this.classNames = 'class1 class2 class3';
      }
    }
    

提交回复
热议问题