You can use the :host(...) selector in combination with @HostBinding() as follows:
@Component({ selector: 'my-comp', styles: ` :host([type-default]) { background-color: red; } :host([type-header]) { background-color: blue; } :host([type-main]) { background-color: green; } ` }) export class MyComponent { @Input() @HostBinding('component-type') componentType:String="type-default" }
and then switch the style like
<header> <my-comp componentType="type-header"></my-comp> </header> <main> <my-comp componentType="type-main"></my-comp> </main> <my-comp></my-comp>
You can also apply the class outside, as in your question, and then use the selector :host(...) , for example
:host(.customStyle1) {
Then you do not need this part
@Input() @HostBinding('component-type') componentType:String="type-default"
but this method can be useful if you want to combine the style with other configuration settings for the component.
source share