Conditional styling on host element

前端 未结 4 2263
既然无缘
既然无缘 2021-02-19 07:04

I have a component that all it does is render , its something like this:

@Component({
     selector: \'my-comp\',
     host: ???,
     template: `
         

        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-19 07:16

    Solution using @HostBinder

    The accepted solution is using the host metadata property which goes against the rules of TSLint:

    TSLint: Use @HostBinding or @HostListener rather than the host metadata property (https://angular.io/styleguide#style-06-03)

    The same can be achieved using @HostBinding instead:

    import { Component, HostBinding, Input } from '@angular/core';
    
    @Component({
      selector: 'my-comp',
      template: `
        
      `
    })
    export default class MyComp {
      @Input() title: string;
      public isChanged: boolean;
      @HostBinding('class.className') get className() { return this.isChanged; }
    }
    

提交回复
热议问题