在新的[[Angular2框架中,是否有人知道像事件一样进行悬停的正确方法?
在Angular1
中有ng-Mouseover
,但这似乎没有被延续。 我浏览了文档,但没有发现任何东西。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中实现功能齐全的悬停事件。
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");
}
正如@Gunter在评论中建议的,还有on-mouseover
的替代项,我们也可以使用它。有些人更喜欢前缀前缀的替代形式,即规范形式。
<div (mouseover)='over()' (mouseout)='out()' style="height:100px; width:100px; background:#e2e2e2">hello mouseover</div>
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找到)
@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)的链接中所述
(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); } }
在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")); } }
@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
中进行了测试
@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
@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)"