HostListener in Angular 17 | Standalone | Listening Events

HostListener  :

        This helps us to handle events more easily and one of the best thing about the Hostlistener is we can handle lost of events.

Here is an example of how to use @HostListener : 

         Now we are create a HostListener for listening how many times the user click's our website.

 Step - 1 :

        We need to create method with functionality for what process we are going to execute when the event triggers.

        clickCount = 0;

  onHostClick() {    this.clickCount++;   }

Step - 2 :

        Use the @HostListener decorator for listen to the events trigger and we need to pass one argument to this decorator of which event we are listening. Like this

        @HostListener('click')
  onHostClick() {     this.clickCount++;   }

Complete Steps :


Output : 



Here are the list of @HostListener events we can handle : 


  1. Mouse Events
    • @HostListener('click', ['$event']) - Fires when the host element is clicked.
    • @HostListener('dblclick', ['$event']) - Fires when the host element is double-clicked.
    • @HostListener('mousedown', ['$event']) - Fires when the mouse button is pressed down on the host element.
    • @HostListener('mouseup', ['$event']) - Fires when the mouse button is released on the host element.
    • @HostListener('mouseover', ['$event']) - Fires when the mouse pointer moves over the host element.
    • @HostListener('mouseout', ['$event']) - Fires when the mouse pointer moves out of the host element.
    • @HostListener('mousemove', ['$event']) - Fires when the mouse pointer moves within the host element.
  2. Keyboard Events
    • @HostListener('keydown', ['$event']) - Fires when a key is pressed down on the host element.
    • @HostListener('keyup', ['$event']) - Fires when a key is released on the host element.
    • @HostListener('keypress', ['$event']) - Fires when a key is pressed and released on the host element.
  3. Window Events
    • @HostListener('window:resize', ['$event']) - Fires when the browser window is resized.
    • @HostListener('window:scroll', ['$event']) - Fires when the browser window is scrolled.
    • @HostListener('window:beforeunload', ['$event']) - Fires before the window, the document, and its resources are about to be unloaded.
  4. Form Events
    • @HostListener('input', ['$event']) - Fires when the value of an <input><select>, or <textarea> element is changed.
    • @HostListener('change', ['$event']) - Fires when the value of an <input><select>, or <textarea> element is committed after changing.
    • @HostListener('submit', ['$event']) - Fires when a form is submitted.
  5. Drag and Drop Events
    • @HostListener('dragstart', ['$event']) - Fires when the user starts dragging an element.
    • @HostListener('dragover', ['$event']) - Fires when a dragged element is over a drop target.
    • @HostListener('drop', ['$event']) - Fires when a dragged element is dropped on a drop target.
  6. Animation Events
    • @HostListener('animationstart', ['$event']) - Fires when an animation starts.
    • @HostListener('animationend', ['$event']) - Fires when an animation completes.
    • @HostListener('animationiteration', ['$event']) - Fires when an animation iteration completes.
  7. Miscellaneous Events
    • @HostListener('focus', ['$event']) - Fires when the host element receives focus.
    • @HostListener('blur', ['$event']) - Fires when the host element loses focus.
    • @HostListener('touchstart', ['$event']) - Fires when a touch point is placed on the touch surface.
    • @HostListener('touchmove', ['$event']) - Fires when a touch point is moved along the touch surface.
    • @HostListener('touchend', ['$event']) - Fires when a touch point is removed from the touch surface.

Comments

Popular posts from this blog

Finding Second largest number in Array - Java - TCS - Technical Interview

Checking Character only in String - Java 2024 - MNC - Technical Interview

Object Class in Java | Article | Solution Maker - Blog