Skip to content

Instantly share code, notes, and snippets.

@arniebradfo
Last active July 25, 2018 15:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arniebradfo/912fc4893d5bf3a38ba63b5f60f7443d to your computer and use it in GitHub Desktop.
Save arniebradfo/912fc4893d5bf3a38ba63b5f60f7443d to your computer and use it in GitHub Desktop.
Angular non destructive class HostBinding
import { Component, Input, HostBinding } from '@angular/core';
/*
<gist-keeps-class class="some classes" [booleanInput]="true" [stringInput]="some-thing"></gist-keeps-class>
will output this:
<gist-keeps-class class="some classes has-boolean some-thing" >
this component keeps class="class" attributes
</gist-keeps-class>
it's not throughly tested, but should work?
*/
@Component({
selector: 'gist-keeps-class',
template: 'this component keeps class="class" attributes'
})
export class KeepsClass {
@Input() booleanInput: boolean = false;
@Input() stringInput: 'some-thing' | 'another-thing' | 'a-third-thing' = 'another-thing';
@Input() class: string = ''; // override the standard class attr with a new one.
@HostBinding('class')
get hostClasses(): string {
return [
this.class, // include our new one
this.booleanInput ? 'has-boolean' : '',
this.stringInput
].join(' ');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment