Angular 2 Hover事件

问题描述 投票:160回答:9

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

Angular1

中有ng-Mouseover,但这似乎没有被延续。 我浏览了文档,但没有发现任何东西。
javascript angular events hover
9个回答
182
投票
如果您想在任何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>

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); } }
您应该同时使用mouseenter和mouseleave事件,以便在角度2中实现功能齐全的悬停事件。

103
投票
是的,在angular2中有on-mouseover,而不是像角度1.x中的ng-Mouseover,因此您必须编写以下内容:-

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

Working Example

正如@Gunter在评论中建议的,还有on-mouseover的替代项,我们也可以使用它。有些人更喜欢前缀前缀的替代形式,即规范形式。

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


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

只需将其适应您的代码(可在https://angular.io/docs/ts/latest/guide/attribute-directives.html找到)

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

如在@Brandon注释中对OP(https://angular.io/docs/ts/latest/guide/attribute-directives.html)的链接中所述

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

在您的HTML中执行:

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

并且在您的组件中执行:

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


7
投票
为了处理事件,您可以尝试这样的操作(对我有用):

在HTML模板中:

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

在角度部分:

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


4
投票
如果选择将鼠标悬停在整个组件上,则可以直接通过@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'; }

它在@angular/core中可用。我在角度4.x.x中进行了测试

2
投票
@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

1
投票
在将要悬停的html的js / ts文件中

@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]) }

在您将要悬停的HTML中

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

在您的将接收悬停信息的js / ts文件中

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

在与捕获的js / ts文件相关的HTML元素中

(elemHovered) = "elemHoveredCatch($event)"

© www.soinside.com 2019 - 2024. All rights reserved.