October 5, 2018
Angular 2 – Set Class of parent element with @HostBindings
With the @HostBinding()
annotation, Angular gives you a useful tool to handle manipulations of css classes of parent Elements.
The advantage is quite clear, the required logic remains in the components class and is not set in the template file. This makes it easier to test and improves the readability of the template file. In addition, it is the only way to set the CSS class of the host element from within the component.
How to use @HostBinding()?
a) Boolean property
The @HostBinding()
annotation can be used on a boolean
property of the component class, or on a method returning a boolean. The boolean value determines whether the class is set or not. If the value/return is true, the class will be applied to the parent component’s DOM Element.
The class to be set is specified within the round brackets, prefixed by class.
–
1 2 3 4 |
isActive:boolean; @HostBinding('class.isActive') isActiveAsMethod(){ return this.isActive; }; |
1 |
@HostBinding('class.isActive') isActive:boolean; |
It is also possible to specify several classes by combining multiple annotations.
1 2 |
@HostBinding('class.isActive') @HostBinding('class.current') isActive:boolean; |
Usage in scss
Since the class is added to the host element it is necessary to use the :host
-selector in your scss file
1 2 3 4 5 |
:host { &.isActive { background-color: red; } } |
b) String property
Additionally, you can set a class directly from a string property to the host component.
1 |
@HostBinding('class') class = 'box'; |
I’ve attached a full example with SCSS styles for the toggled class:
1 2 3 4 |
<p> box works! </p> <button (click)="click()">Toggle</button> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
:host { width: 200px; height: 200px; display: block; background-color: blue; padding: 20px; color: #ffffff; text-align: center; font-weight: bold; &.isActive { background-color: red; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import {Component, HostBinding, OnInit} from '@angular/core'; @Component({ selector: 'app-box', templateUrl: './box.component.html', styleUrls: ['./box.component.scss'] }) export class BoxComponent implements OnInit { @HostBinding('class.isActive') isActive: boolean; @HostBinding('class') class = 'box'; constructor() {} ngOnInit() { } click() { this.isActive = ! this.isActive; } } |
This post has been reviewed and checked for compatibility with the latest Angular Version, it was originally created in Feb 2017!
is this.isActive = !this.isActive;
no this.isActive =! this.isActive;
For me, it’s not working with the provided SCSS syntax for adding style to the host element with class .isActive. If I’m not wrong, you specify style for all SUB element with this class. Instead, add the “&” sign like this:
:host {
&.isActive {
}
}
Pardon, you are right this was a typo (fixed). Thanks for your comment!
Fyi: another way of writing would be .host(.isActive)
You should use a
getter
instead of amethod
. When using amethod
the css classes will always be set.Here an example, this will work
@Aico Klein Ovink
Why is it always set when using a normal method?
Using
@HostBinding('class') class = 'box'
prevents extra classes being applied to your. So that
will *NOT* work.