Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the second argument to HostListener for and why is it often given as ['$event']?

Angular's HostListener decorator takes two arguments. The first specifies the name of event to listen for. The second is an optional array of strings unilluminatingly named args. Naturally, its meaning isn't currently explained in the docs (which currently devote an entire four words to documenting the HostListener decorator, the first two of which state that it is, uh, the "HostListener decorator").

I've only ever seen HostListener called in two ways:

  1. Omitting the args argument entirely (e.g. at https://angular.io/guide/styleguide#style-06-01)
  2. Specifying the args argument as ['$event'], e.g. at https://angular-2-training-book.rangle.io/handout/advanced-angular/directives/listening_to_an_element_host.html

What does this mysterious args parameter do, and why is it sometimes specified as ['$event']?

like image 537
Mark Amery Avatar asked Nov 07 '17 11:11

Mark Amery


People also ask

What is the difference between hostbinding and hostlistener?

The HostBinding & HostListener are decorators in Angular. HostListener listens to host events, while HostBinding allows us to bind to a property of the host element. The host is an element on which we attach our component or directive.

How to turn an element into a host element using @hostlistener?

As described before, this directive will turn any element that it is placed on into its host element and it set its mouseenter event listener on the host element through @HostListener . As you can see in the code, you would declare the @HostListener decorator method with the DOM event passed in as the argument, followed by its event handler method.

Can I use pseudo-events with @hostlistener?

Having many directives that listen to the same event on the global elements could eventually hurt the performance of your application. Lastly, just like in Angular event binding, you can also use Angular Pseudo-Events with @HostListener.

What is @hostlistener in angular?

What is @hostListener in Angular? 1 It is a decorator which specifies the DOM Event to be captured 2 Specifies event handler which needs to be invoked on the event call 3 @hostListener receives Event Name and Callback Function associated 4 @hostListner can capture event info triggered by user More ...


1 Answers

$event is the reserved name for the actual event value like it can be used in (click)="clickHandler($event)" event bindings.

@HostListener('eventName', args) supports an array of values like

['$event.target.value', '$event.name']

that specifies what values will be passed as parameters to the event handler.

It looks like just always passing $event (assuming ['$event'] as default) would be a more reasonable approach,
but if WebWorker is used, this way the amount of data passed between UI thread and WebWorker thread can reduced by only passing that part(s) of the event that are actually required by the event handler (or no value at all if the parameter is omitted).

See also https://angular.io/api/core/HostListener#args

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

Günter Zöchbauer