Skip to content

Instantly share code, notes, and snippets.

@riapacheco
Created July 20, 2022 18:04
Show Gist options
  • Save riapacheco/06ad3c0ee3550215af7a36bd99ad865a to your computer and use it in GitHub Desktop.
Save riapacheco/06ad3c0ee3550215af7a36bd99ad865a to your computer and use it in GitHub Desktop.
Conditionally add class to :host{} element via @HostBinding
:host {
background-color: white;
}
:host(.change-bg) {
background-color: red;
}
export class AddClassToHostComponent implements OnInit {
// Behavior driving property
conditionalProperty = false;
@HostBinding(class.change-bg) changeBg = false;
constructor() {}
ngOnInit() {
if (this.conditionalProperty) {
this.changeBg = true;
} else { this.changeBg = false; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment