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

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

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


当前回答

如果鼠标在整个组件上是你的选项,你可以直接是@hostListener来处理事件来执行鼠标在下面的al。

  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中可用。我在angular 4.x.x中测试了它

其他回答

如果你只是想要一个悬停效果,请使用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>

如果鼠标在整个组件上是你的选项,你可以直接是@hostListener来处理事件来执行鼠标在下面的al。

  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中可用。我在angular 4.x.x中测试了它

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

如果你对鼠标进出某个组件感兴趣,你可以使用@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)中的链接所解释的那样

在你的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)"