Javascript Angular 2 悬停事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37686772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 2 Hover event
提问by Ronin
In the new Angular2framework, does anyone know the proper way to do a hover like an event?
在新的Angular2框架中,有没有人知道像事件一样悬停的正确方法?
In Angular1there was ng-Mouseover
, but that doesn't seem to have been carried over.
在Angular1 中有ng-Mouseover
,但似乎没有被继承。
I've looked through the docs and haven't found anything.
我浏览了文档,没有找到任何东西。
回答by Vikash Dahiya
If you want to perform a hover like event on any HTML element, then you can do it like this.
如果你想在任何 HTML 元素上执行类似悬停的事件,那么你可以这样做。
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);
}
}
You should use both mouseenter and mouseleave events inorder to implement fully functional hover events in angular 2.
您应该同时使用 mouseenter 和 mouseleave 事件,以便在 angular 2 中实现功能齐全的悬停事件。
回答by Pardeep Jain
yes there is on-mouseover
in angular2 instead of ng-Mouseover
like in angular 1.x so you have to write this :-
是的,on-mouseover
在 angular2 中而不是ng-Mouseover
在 angular 1.x 中,所以你必须这样写:-
<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.
正如@Gunter 在评论中建议的那样,on-mouseover
我们也可以使用它。有些人更喜欢 on- 前缀替代,称为规范形式。
Update
更新
HTML Code -
HTML 代码 -
<div (mouseover)='over()' (mouseout)='out()' style="height:100px; width:100px; background:#e2e2e2">hello mouseover</div>
Controller/.TS Code -
控制器/.TS 代码 -
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");
}
}
Some other Mouse events can be used in Angular -
Angular 中还可以使用其他一些鼠标事件 -
(mouseenter)="myMethod()"
(mousedown)="myMethod()"
(mouseup)="myMethod()"
回答by bene
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)
只需使其适应您的代码(可在以下位置找到:https: //angular.io/docs/ts/latest/guide/attribute-directives.html)
回答by paul
If you are interested in the mouse entering or leaving one of your components you can use the @HostListener
decorator:
如果您对鼠标进入或离开您的组件之一感兴趣,您可以使用@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);
}
...
}
As explained in the link in @Brandon comment to OP (https://angular.io/docs/ts/latest/guide/attribute-directives.html)
正如@Brandon 对 OP 的评论中的链接所解释的(https://angular.io/docs/ts/latest/guide/attribute-directives.html)
回答by Alireza
Simply do (mouseenter)
attribute in Angular2+...
只需(mouseenter)
在 Angular2+ 中做属性...
In your HTML do:
在你的 HTML 中做:
<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);
}
}
回答by Dudi
For handling the event on overing, you can try something like this (it works for me):
为了处理overing事件,你可以尝试这样的事情(它对我有用):
In the Html template:
在 Html 模板中:
<div (mouseenter)="onHovering($event)" (mouseleave)="onUnovering($event)">
<img src="../../../contents/ctm-icons/alarm.svg" class="centering-me" alt="Alerts" />
</div>
In the angular component:
在角度分量中:
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"));
}
}
回答by Aniruddha Das
If the mouse over for all over the component is your option, you can directly is @hostListener
to handle the events to perform the mouse over al below.
如果鼠标悬停在整个组件上是你的选择,你可以直接@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';
}
Its available in @angular/core
. I tested it in angular 4.x.x
它在@angular/core
. 我以角度测试了它4.x.x
回答by Clayton K. N. Passos
@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>`,
})
回答by Lyudmil Petrov
In your js/ts file for the html that will be hovered
在将悬停的 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])
}
In your HTML that will be hovered
在您将悬停的 HTML 中
(mouseenter) = "onHoverEnter()" (mouseleave)="onHoverLeave()"
In your js/ts file that will receive info of the hovering
在将接收悬停信息的 js/ts 文件中
elemHoveredCatch(d): void {
console.log(d)
}
In your HTML element that is connected with catching js/ts file
在与捕获 js/ts 文件相关联的 HTML 元素中
(elemHovered) = "elemHoveredCatch($event)"