Angular 2 Hover event

前端 未结 9 1166
深忆病人
深忆病人 2020-12-02 06:20

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

In Angular1 there was ng-Mouseov

9条回答
  •  生来不讨喜
    2020-12-02 06:43

    If you are interested in the mouse entering or leaving one of your components you can use the @HostListener decorator:

    import { Component, HostListener, OnInit } from '@angular/core';
    
    @Component({
      selector: 'my-component',
      templateUrl: './my-component.html',
      styleUrls: ['./my-component.scss']
    })
    export class MyComponent implements OnInit {
    
      @HostListener('mouseenter') 
      onMouseEnter() {
        this.highlight('yellow');
      }
    
      @HostListener('mouseleave') 
      onMouseLeave() {
        this.highlight(null);
      }
    
    ...
    
    }
    

    As explained in the link in @Brandon comment to OP (https://angular.io/docs/ts/latest/guide/attribute-directives.html)

提交回复
热议问题