Angular 2 Hover event

前端 未结 9 1144
深忆病人
深忆病人 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:34

    In your js/ts file for the html that will be hovered

    @Output() elemHovered: EventEmitter<any> = new EventEmitter<any>();
    onHoverEnter(): void {
        this.elemHovered.emit([`The button was entered!`,this.event]);
    }
    
    onHoverLeave(): void {
        this.elemHovered.emit([`The button was left!`,this.event])
    }
    

    In your HTML that will be hovered

     (mouseenter) = "onHoverEnter()" (mouseleave)="onHoverLeave()"
    

    In your js/ts file that will receive info of the hovering

    elemHoveredCatch(d): void {
        console.log(d)
    }
    

    In your HTML element that is connected with catching js/ts file

    (elemHovered) = "elemHoveredCatch($event)"
    
    0 讨论(0)
  • 2020-12-02 06:38

    You can do it with a host:

    import { Directive, ElementRef, Input } from '@angular/core';
    
    @Directive({
      selector: '[myHighlight]',
      host: {
        '(mouseenter)': 'onMouseEnter()',
        '(mouseleave)': 'onMouseLeave()'
      }
    })
    export class HighlightDirective {
      private _defaultColor = 'blue';
      private el: HTMLElement;
    
      constructor(el: ElementRef) { this.el = el.nativeElement; }
    
      @Input('myHighlight') highlightColor: string;
    
      onMouseEnter() { this.highlight(this.highlightColor || this._defaultColor); }
      onMouseLeave() { this.highlight(null); }
    
       private highlight(color:string) {
        this.el.style.backgroundColor = color;
      }
    
    }
    

    Just adapt it to your code (found at: https://angular.io/docs/ts/latest/guide/attribute-directives.html )

    0 讨论(0)
  • 2020-12-02 06:39
    @Component({
        selector: 'drag-drop',
        template: `
            <h1>Drag 'n Drop</h1>
            <div #container 
                 class="container"
                 (mousemove)="onMouseMove( container)">
                <div #draggable 
                     class="draggable"
                     (mousedown)="onMouseButton( container)"
                     (mouseup)="onMouseButton( container)">
                </div>
            </div>`,
    
    })
    

    http://lishman.io/angular-2-event-binding

    0 讨论(0)
  • 2020-12-02 06:40

    yes there is on-mouseover in angular2 instead of ng-Mouseover like in angular 1.x so you have to write this :-

    <div on-mouseover='over()' style="height:100px; width:100px; background:#e2e2e2">hello mouseover</div>
    
    over(){
        console.log("Mouseover called");
      }
    

    As @Gunter Suggested in comment there is alternate of on-mouseover we can use this too. Some people prefer the on- prefix alternative, known as the canonical form.

    Update

    HTML Code -

    <div (mouseover)='over()' (mouseout)='out()' style="height:100px; width:100px; background:#e2e2e2">hello mouseover</div>
    

    Controller/.TS Code -

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      name = 'Angular';
    
      over(){
        console.log("Mouseover called");
      }
    
      out(){
        console.log("Mouseout called");
      }
    }
    

    Working Example

    Some other Mouse events can be used in Angular -

    (mouseenter)="myMethod()"
    (mousedown)="myMethod()"
    (mouseup)="myMethod()"
    
    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-12-02 06:46

    Simply do (mouseenter) attribute in Angular2+...

    In your HTML do:

    <div (mouseenter)="mouseHover($event)">Hover!</div> 
    

    and in your component do:

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'component',
      templateUrl: './component.html',
      styleUrls: ['./component.scss']
    })
    
    export class MyComponent implements OnInit {
    
      mouseHover(e) {
        console.log('hovered', e);
      }
    } 
    
    0 讨论(0)
提交回复
热议问题