Angular 2 Hover事件

Ron*_*nin 166 javascript events hover angular

在新的Angular2框架中,有没有人知道像事件一样悬停的正确方法?

Angular1中ng-Mouseover,但似乎没有结转.

我查看了文档但没有找到任何内容.

Vik*_*iya 188

如果您想在任何HTML元素上执行像事件一样的悬停,那么您可以这样做.

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>
Run Code Online (Sandbox Code Playgroud)

零件

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);
   }
}
Run Code Online (Sandbox Code Playgroud)

您应该同时使用mouseenter和mouseleave事件,以便在角度2中实现功能完整的悬停事件.


Par*_*ain 104

是的,在角度1.x中有角度on-mouseover而不是ng-Mouseover像1.x所以你必须这样写: -

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

over(){
    console.log("Mouseover called");
  }
Run Code Online (Sandbox Code Playgroud)

Working Example

正如@Gunter在评论中建议的那样on-mouseover我们也可以使用它.有些人更喜欢on-prefix替代品,称为规范形式.

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

  • 为什么不``<div(mouseover)='over()'`?;-) (44认同)
  • 这些不是Angular事件,而是浏览器事件. (5认同)
  • @GünterZöchbauer,他们是所有活动的清单吗?我在Angular 2网站上查看,但找不到(鼠标悬停) (2认同)
  • 显然这是方法,但是有人有指向 Angular 文档的链接吗?我觉得它超级抽象和稀疏。我只是在寻找任何内容的列表,所以我知道什么是标准的。 (2认同)

ben*_*ene 30

您可以使用主机执行此操作:

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;
  }

}
Run Code Online (Sandbox Code Playgroud)

只需根据您的代码进行调整(参见:https://angular.io/docs/ts/latest/guide/attribute-directives.html)


pau*_*aul 15

如果您对鼠标进入或离开某个组件感兴趣,可以使用@HostListener装饰器:

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);
  }

...

}
Run Code Online (Sandbox Code Playgroud)

正如@Brandon对OP的评论中所解释的那样(https://angular.io/docs/ts/latest/guide/attribute-directives.html)


Ali*_*eza 10

只需(mouseenter)在Angular2 +中执行属性...

在你的HTML中:

<div (mouseenter)="mouseHover($event)">Hover!</div> 
Run Code Online (Sandbox Code Playgroud)

并在您的组件中执行:

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);
  }
} 
Run Code Online (Sandbox Code Playgroud)


Dud*_*udi 7

为了处理覆盖事件,您可以尝试这样的事情(它适用于我):

在Html模板中:

<div (mouseenter)="onHovering($event)" (mouseleave)="onUnovering($event)">
  <img src="../../../contents/ctm-icons/alarm.svg" class="centering-me" alt="Alerts" />
</div>
Run Code Online (Sandbox Code Playgroud)

在角度分量中:

    onHovering(eventObject) {
    console.log("AlertsBtnComponent.onHovering:");
    var regExp = new RegExp(".svg" + "$");

    var srcObj = eventObject.target.offsetParent.children["0"];
    if (srcObj.tagName == "IMG") {
        srcObj.setAttribute("src", srcObj.getAttribute("src").replace(regExp, "_h.svg"));       
    }

   }
   onUnovering(eventObject) {
    console.log("AlertsBtnComponent.onUnovering:");
    var regExp = new RegExp("_h.svg" + "$");

    var srcObj = eventObject.target.offsetParent.children["0"];
    if (srcObj.tagName == "IMG") {
        srcObj.setAttribute("src", srcObj.getAttribute("src").replace(regExp, ".svg"));
    }
}
Run Code Online (Sandbox Code Playgroud)


Ani*_*Das 6

如果鼠标悬停在整个组件上是你的选择,你可以直接@hostListener处理事件来执行鼠标悬停在下面的所有部分。

  import {HostListener} from '@angular/core';

  @HostListener('mouseenter') onMouseEnter() {
    this.hover = true;
    this.elementRef.nativeElement.addClass = 'edit';
  }

  @HostListener('mouseleave') onMouseLeave() {
    this.hover = false;
    this.elementRef.nativeElement.addClass = 'un-edit';
  }
Run Code Online (Sandbox Code Playgroud)

它在@angular/core. 我以角度测试了它4.x.x