Last active
July 25, 2018 15:34
-
-
Save arniebradfo/912fc4893d5bf3a38ba63b5f60f7443d to your computer and use it in GitHub Desktop.
Angular non destructive class HostBinding
This file contains 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
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