I know that in Angular2 I can add a class 'red' to a component's selector element by doing this:
@Component({
selector: 'selector-el',
host: {
'[class.red]': 'true'
},
...
})
I'm wondering whether there's a way to add a dynamic class to a host, similar to what you would do with NgClass (I know NgClass is not actually supported, I'm looking for possible solutions):
@Component({
selector: 'selector-el',
host: {
'[NgClass]': 'colorClass'
},
...
})
...
constructor(){
this.colorClass = 'red';
}
You can simply add @HostBinding('class') class = 'someClass'; inside your @Component class. The className directive may also be used and it is better to avoid using class as a variable name (since you might reference it and change it later). Example: @HostBinding('className') myTheme = 'theme-dark'; .
Adding a dynamic class name is as simple as adding the prop :class="classname" to your component. Whatever classname evaluates to will be the class name that is added to your component.
HostBindinglinkDecorator that marks a DOM property as a host-binding property and supplies configuration metadata. Angular automatically checks host property bindings during change detection, and if a binding changes it updates the host element of the directive.
Every component is associated within an element that matches the component's selector. This element, into which the template is rendered, is called the host element. The :host pseudo-class selector may be used to create styles that target the host element itself, as opposed to targeting elements inside the host.
The Renderer
s setElementClass
can be used to add or remove an arbitrary class. For example md-[color]
where color
is provided by an input
<some-cmp [color]="red">
@Component({
// @Directive({
selector: 'some-cmp',
template: '...'
})
export class SomeComp {
_color: string;
@Input()
set color(color: string) {
this._color = color;
this.renderer.setElementClass(this.elementRef.nativeElement, 'md-' + this._color, true);
}
get color(): string {
return this._color;
}
constructor(private elementRef: ElementRef, private renderer: Renderer){}
}
See also nativeElement.classList.add() alternative
You can use something like that:
@Directive({
(...)
host: {
'[class.className]' : 'className',
'[class]' : 'classNames'
}
}
export class MyDirective {
className: boolean;
classNames: string;
constructor() {
this.className = true;
this.classNames = 'class1 class2 class3';
}
}
If you like to change it from outside you can combine @HostBinding
and @Input()
:
@Component({
selector: 'my-component',
template: ``
})
export class MyComponent {
@HostBinding('class.your-class') @Input() isSelected: boolean;
}
I did it in this way. Maybe someone will come in handy
@HostBinding('class') get hostClasses() {
return `some-class ${this.dynamicOne} ${this.disabled ? 'disabled' : ''}`;
}
OR Simon_Weaver's Suggestion: (the return value can also be an array
, Thank you!)
@HostBinding('class') get hostClasses() {
const classList = ['some-class', this.dynamicOne];
if( this.disabled) { classList.push('disabled'); }
return classList;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With