Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: @HostBinding or host: {}?

I was wondering if there was a drastic difference (and if there is, what is it?) between using @HostBinding and the host attribute of the component ?

I have been asking myself that question while I was using animations because I was in these cases (which look rather close) :

@Component({
  selector: 'mycomponent',
  animations: [
    trigger('myTransition', [
      state('inactive', style({
      backgroundColor: '#eee',
      transform: 'scale(1)'
    })),
    state('active',   style({
      backgroundColor: '#cfd8dc',
      transform: 'scale(1.1)'
    })),
    transition('inactive => active', animate('100ms ease-in')),
    transition('active => inactive', animate('100ms ease-out'))
  ])],
  host: {
    '[@myTransition]': '',
   },
})

OR

@Component({
  selector: 'mycomponent',
  animations: [
    trigger('myTransition', [
      state('inactive', style({
      backgroundColor: '#eee',
      transform: 'scale(1)'
    })),
    state('active',   style({
      backgroundColor: '#cfd8dc',
      transform: 'scale(1.1)'
    })),
    transition('inactive => active', animate('100ms ease-in')),
    transition('active => inactive', animate('100ms ease-out'))
  ])],
})

export class MyComponent {
  @HostBinding('@myTransition') get myTransition() {
    return '';
  }
}

I then thought that it my might be the new way of host binding.

Thanks in advance for your advice and input ;)

like image 557
nicolasrigaudiere Avatar asked Mar 06 '17 17:03

nicolasrigaudiere


People also ask

What is @hostbinding and @hostlistener 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 on style?

Host Binding binds a Host element property to a variable in the directive or component The following appHighLight directive, uses the HostBinding on style.border property of the parent element to the border property.

What is @hostlistener decorator in angular?

@HostListener() Decorator 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.

How to manipulate the host element by the angular custom directive?

We can use @HostBinding, @HostListerner decorator, renderer2, and ElementRef to communicate and manipulate the appearance or behavior of the host element by the angular custom directive. It is not recommended to manipulate the DOM directly with ElementRef class, because of security reasons.


2 Answers

Both are equivalent.
In ES5 where decorators are not available, you can use host: {}

like image 154
Günter Zöchbauer Avatar answered Oct 02 '22 15:10

Günter Zöchbauer


The official guidance is to prefer HostListener/HostBinding

from the Angular style guide

HostListener/HostBinding decorators versus host metadata

Style 06-03 Consider preferring the @HostListener and @HostBinding to the host property of the @Directive and @Component decorators.

Do be consistent in your choice.

Why? The property associated with @HostBinding or the method associated with @HostListener can be modified only in a single place—in the directive's class. If you use the host metadata property, you must modify both the property declaration inside the controller, and the metadata associated with the directive.

However, the angular/material2 project says to prefer "host"

Host bindings

Prefer using the host object in the directive configuration instead of @HostBinding and @HostListener. We do this because TypeScript preserves the type information of methods with decorators, and when one of the arguments for the method is a native Event type, this preserved type information can lead to runtime errors in non-browser environments (e.g., server-side pre-rendering).

like image 31
Rip Ryness Avatar answered Oct 02 '22 16:10

Rip Ryness