javascriptangulareventshover

Angular 2 Hover event


In the new Angular2 framework, does anyone know the proper way to do a hover like an event?

In Angular1 there was ng-Mouseover, but that doesn't seem to have been carried over.

I've looked through the docs and haven't found anything.


Solution

  • If you want to perform a hover like event on any HTML element, then you can do it like this.

    HTML

     <div (mouseenter) ="mouseEnter('div a') "  (mouseleave) ="mouseLeave('div A')">
            <h2>Div A</h2>
     </div> 
     <div (mouseenter) ="mouseEnter('div b')"  (mouseleave) ="mouseLeave('div B')">
            <h2>Div B</h2>
     </div>
    

    Component

    import { Component } from '@angular/core';
    
    @Component({
        moduleId: module.id,
        selector: 'basic-detail',
        templateUrl: 'basic.component.html',
    })
    export class BasicComponent{
    
       mouseEnter(div : string){
          console.log("mouse enter : " + div);
       }
    
       mouseLeave(div : string){
         console.log('mouse leave :' + div);
       }
    }
    

    You should use both mouseenter and mouseleave events in order to fully implement functional hover events in angular 2.