Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding how @HostBinding works in angular for toggleClass directive

I came across the following angular directive:

import { Directive , HostListener , HostBinding } from '@angular/core';

@Directive({
    selector: '[appDropdown]'
})

export class DropdownDirective {
    @HostBinding('class.open') isOpen = false;

    @HostListener('click') toggleOpen() {
        this.isOpen = !this.isOpen;
    }
}

While surfing code online , basically the code only toggles the open class on the element the directive is used on , so this directive can be used like so:

<ul class="nav navbar-nav navbar-right">
    <li class="dropdown" appDropdown>
        <a href="#" class="dropdown-toggle" role="button">Manage <span class="caret"></span></a>
        <ul class="dropdown-menu">
            <li><a href="#">Save Data</a></li>
            <li><a href="#">Fetch Data</a></li>
        </ul>
    </li>
</ul>

Now what i don't understand is the below line of code in the directive:

 @HostBinding('class.open') isOpen = false;

Whats exactly is it doing and how exactly does it function ? I am not quite understanding the above line of code. can somebody please explain ?

like image 239
Alexander Solonik Avatar asked Oct 30 '17 18:10

Alexander Solonik


People also ask

What is @HostBinding in Angular?

@HostBinding and @HostListener are two decorators in Angular that can be really useful in custom directives. @HostBinding lets you set properties on the element or component that hosts the directive, and @HostListener lets you listen for events on the host element or component.

What is HostBinding decorator doing in this directive?

HostBindinglink Decorator 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.

How does host binding work?

HostBinding - Declares a host property binding. Angular automatically checks host property bindings during change detection. If a binding changes, it will update the host element of the directive. @HostBinding - will bind the property to the host element, If a binding changes, HostBinding will update the host element.

What is HostListener in Angular with example?

HostListenerlinkDecorator that declares a DOM event to listen for, and provides a handler method to run when that event occurs.

What is the use of @hostbinding in angular?

In Angular, the @HostBinding () function decorator allows you to set the properties of the host element from the directive class. Let's say you want to change the style properties such as height, width, color, margin, border, etc., or any other internal properties of the host element in the directive class.

How to bind a class to the host element using @hostbinding?

The @HostBinding decorator takes one parameter, the name of the property on the host element which we want to bind to. If you remember we can use the alternative ngClass syntax by binding to the [class.<class-name>] property. Let’s add the card-outline-primary class to our host element when the ishovering boolean is true.

How do I use the Rainbow directive with @hostbinding?

Our Rainbow directive uses two @HostBinding decorators to define two class members, one that’s attached to the host’s style.color binding and the other to style.border-color. You can also bind to any class, property, or attribute of the host. The @HostListener with the 'keydown' argument listens for the keydown event on the host.

How to change the color of the host element in angular?

Right now, the appChbgcolor directive will change the color of the host element. In Angular, the @HostListener () function decorator allows you to handle events of the host element in the directive class. Let's take the following requirement: when you hover you mouse over the host element, only the color of the host element should change.


2 Answers

@HostBinding allows you to define a binding for a host element of your directive. As you probably know there's a special binding syntax for the class that looks like this:

<element [class.class-name]="expression">...</element>

You can read specifics of implementation in How [class] [attr] [style] directives work.

In your example the li element is the host element and the expression is isOpen so the host binding your referenced essentially defines the following:

<li class="dropdown" appDropdown [class.open]="isOpen">
like image 176
Max Koretskyi Avatar answered Nov 28 '22 15:11

Max Koretskyi


Have a look at this link. To sum it up:

@HostBinding decorator lets you update properties on the host element of the directive. What that means, as far as i understand, is that Angular change detection will evaluate the variables (expresions) decorated by it and propagate changes to the attributes you 'bind' in the host element (add\remove class in your case).

like image 39
Fjut Avatar answered Nov 28 '22 15:11

Fjut