Angular 2: How to style host element of the component?

前端 未结 6 2087
余生分开走
余生分开走 2020-11-28 01:54

I have component in Angular 2 called my-comp:


How does one style the host element of this component in Angu

6条回答
  •  时光说笑
    2020-11-28 02:17

    I have found a solution how to style just the component element. I have not found any documentation how it works, but you can put attributes values into the component directive, under the 'host' property like this:

    @Component({
        ...
        styles: [`
          :host {
            'style': 'display: table; height: 100%',
            'class': 'myClass'
          }`
    })
    export class MyComponent
    {
        constructor() {}
    
        // Also you can use @HostBinding decorator
        @HostBinding('style.background-color') public color: string = 'lime';
        @HostBinding('class.highlighted') public highlighted: boolean = true;
    }
    

    UPDATE: As Günter Zöchbauer mentioned, there was a bug, and now you can style the host element even in css file, like this:

    :host{ ... }
    

提交回复
热议问题