Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Is there list of HostListener-Events?

Tags:

angular

I am using a hostlistener in a directive to detect "blur"- and "keyup"-events. Now I need to detect changes in the input-element the directive sits on. I tried

@HostListener('change', ['$event'])

but it does not fire.

Is there a "change"-event? I have also read, that there should be an "input"-event, but that does not fire either.

So, my question is: Is there a list of available events that I can use?

I have searched google:

https://www.google.de/search?q=angular+2+list+of+hostlistener+events

and the angular-documentation:

https://angular.io/api/core/HostListener

but did not find anything.

like image 210
Tobias Gassmann Avatar asked Dec 12 '17 11:12

Tobias Gassmann


People also ask

What are the HostListener events in Angular?

@HostListener is Angular's decorator method that's used for listening to DOM events on the host element of both component and attribute directives. @HostListener sets the listeners once the directive is initialized and removes them automatically once the directive gets destroyed.

Can we use HostListener in component?

Introduction. @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.

How do I use HostListener?

We apply the directive on a host element ( p in the example) as shown below. Whenever the mouse is moved over the p element, the mouseover event is captured by the HostListener. It runs the onMouseOver method which we have attached to it. This method adds a green border to the p element using the HostBinding.

Which function should we use to add event listener in a directive?

The ngOn directive adds an event listener to a DOM element via angular. element(). on(), and evaluates an expression when the event is fired.


2 Answers

Open angular dom element schema https://github.com/angular/angular/blob/master/packages/compiler/src/schema/dom_element_schema_registry.ts#L78

where:

  • (no prefix): property is a string.
  • *: property represents an event.
  • !: property is a boolean.
  • #: property is a number.
  • %: property is an object.

Then press ctrl+F and write *

enter image description here

@HostListener(and also (customEvent)="handler()") can also listen to custom events

Example

like image 56
yurzui Avatar answered Oct 21 '22 15:10

yurzui


The list of events you can listen to can be found here

https://www.w3schools.com/jsref/dom_obj_event.asp

and I believe this one is the same, but better organized (I'm not sure if all are valid)

https://developer.mozilla.org/en-US/docs/Web/Events

like image 28
Kacper Wolkowski Avatar answered Oct 21 '22 15:10

Kacper Wolkowski