如何将 class(es) 添加到悬停时由 ngFor 生成的元素?

How to add class(es) to elements generated by ngFor on hover?

问题:我有一些html使用*ngFor生成一堆div,都具有相同的class。我希望能够向单个元素添加活动 class。尽管将 class 应用于我的所有元素而不是仅一个元素,但我将 运行 保留在问题中。

HTML:

  <div class="feed" infiniteScroll [infiniteScrollDistance]="2" [infiniteScrollThrottle]="50" (scrolled)="onScroll()" >
    <div class="mentions" *ngFor = 'let item of feed; let i = index;'>
    <div class="content-wrapper">
      <img src={{item.image}}>
      <div class="content-text">
        <div>{{item.name}}</div><br>
        <div>{{item.text}}</div><br>
        <div>{{item.followers}}</div><br>
      </div>
    </div>
    <div class="votebtn"> 
<button mat-button matSuffix mat-icon-button aria-label="UpVote" id = u-{{item.source}} class="UpVote" (click)="vote(i, item, 'keep')">
      <mat-icon>thumb_up</mat-icon>
    </button>
<button mat-button matSuffix mat-icon-button aria-label="DownVote" id=d-{{item.source}} class=DownVote (click)="vote(i, item, 'ignore')">
    <mat-icon>thumb_down</mat-icon>
    </button>
    </div>
    </div>
</div>

目标: 最后,我希望能够做一些简单的事情,例如在鼠标悬停时将 box-shadow: 1px -1px 12px 0px #c9c9c9 添加到单个 mentions 元素然后在鼠标移开时将其删除。这是我最近的尝试。

    <div *ngFor = 'let item of feed; let i = index;' [ngClass] = 'state' (mouseover) = "state = 'mentions hover'" (mouseout) = "state = 'mentions'">

同样,它会添加到所有元素中。

您可以通过定义一个小的属性指令来存档此视觉效果:

    @Directive({
      selector: '[ngHoverClass]'
    })
    export class NgHoverClassDirective {
       @Input(`ngHoverClass`)
       set classesToAply(v: string | string[]){
          this.classes = Array.isArray(v) ? v: v.split(' ');
       }
       private classes: string[] = [];

       constructor(private renderer: Renderer2, private element: ElementRef){}

       @HostListener('mouseover')
       onHover(){
         this.classes.forEach(c=> this.renderer.addClass(this.element.nativeElement, c));
       }

       @HostListener('mouseleave')
       onLeave(){
         this.classes.forEach(c=> this.renderer.removeClass(this.element.nativeElement, c));
       }
    }

使其可供使用后,您可以在组件中使用它,如下所示:

<div *ngFor = 'let item of feed; let i = index;' ngHoverClass='hover' class="mentions">

回复晚了。我让对象数组完成大部分工作,因为这种模式对我来说更干净:

CSS

 li.selected {
   border-bottom: thick solid purple;
 }
 li.hover {
   border-bottom: thick dotted gray;
 }

对象

 NavigtionMenu = [
   {
     name: 'Home',
     link: 'path/to/link',
     hover: false,
     selected: false,
   },
   {
     name: 'About Us',
     link: 'path/to/link',
     hover: false,
     selected: false,
   }
 ]

模板

  <li 
     *ngFor="let item of items; let i = index"
     (mouseover)="item.hover = true"
     (mouseout)="item.hover = false"
     (click)="item.selected = true"
     [routerLink]="item.link"
     [ngClass]="{
       'selected' : link.selected === true && link.hover === false,
       'hover'    : link.hover === true
     }"
  >
   {{ item.name }}
  </li>

 <div *ngFor="let item of feed; let i = index;" [ngClass]="{'hovered': item.isHovered'}" (mouseover)="item.isHovered=true" (mouseout)="item.isHovered=false">