在新的Angular2框架中,有人知道如何像事件一样执行悬停吗?

在Angular1中有ng-Mouseover,但这似乎没有被延续。

我看了所有的文件,什么都没发现。


当前回答

在你的js/ts文件的html将被盘旋

@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)"

其他回答

在你的js/ts文件的html将被盘旋

@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)"

如果你只是想要一个悬停效果,请使用hover .css。

NPM我盘旋。css 在文件angular中。Json属性projects.architect.build.options.styles——>添加这一行到数组:node_modules/hover.css/scss/hover.scss

在你想要效果的元素上使用它们的任何类,即:

<div *ngFor="let source of sources">
    <div class="row justify-content-center">
        <div class="col-12 hvr-glow">
            <!-- My content -->
        </div>
    </div>
</div>
@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

对于在overing上处理事件,您可以尝试这样做 (这对我很管用):

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

是的,angular2中有on-mouseover,而不是angular 1中的ng-Mouseover。所以你必须这样写:-

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

over(){
    console.log("Mouseover called");
  }

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

更新

HTML代码-

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

控制器/。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");
  }
}

工作示例

其他一些鼠标事件也可以在Angular中使用

(mouseenter)="myMethod()"
(mousedown)="myMethod()"
(mouseup)="myMethod()"