Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add a dynamic class to host in Angular 2?

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';
}
like image 893
Alec Sibilia Avatar asked Mar 14 '16 19:03

Alec Sibilia


People also ask

How do I add a class to my host?

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'; .

How do I add a class to a component?

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.

What is HostBinding in Angular?

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.

What is host selector in Angular?

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.


4 Answers

The Renderers 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

like image 175
Günter Zöchbauer Avatar answered Sep 21 '22 05:09

Günter Zöchbauer


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';
  }
}
like image 42
Thierry Templier Avatar answered Sep 22 '22 05:09

Thierry Templier


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;
}
like image 32
crashbus Avatar answered Sep 20 '22 05:09

crashbus


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;
}
like image 45
Ashot Aleqsanyan Avatar answered Sep 18 '22 05:09

Ashot Aleqsanyan