Created
          July 20, 2022 18:04 
        
      - 
      
 - 
        
Save riapacheco/06ad3c0ee3550215af7a36bd99ad865a to your computer and use it in GitHub Desktop.  
    Conditionally add class to :host{} element via @HostBinding
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | :host { | |
| background-color: white; | |
| } | |
| :host(.change-bg) { | |
| background-color: red; | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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