Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(mouseenter) event on individual item in *ngFor - angular 4

So my goal is to create a mouseenter/mouseexit event for an individual li's child elements. For my app, when a user mouseenters an li elememt, it's child element div class='thumbs' is added to the DOM through a component boolean property called "hover" -- *"ngIf='hover'"

My problem is, when I mousenter over an indivual li elememt, all the thumb icons are shown instead of the just the individual li's thumb icons.

Here is a video higlighting my problem:

enter image description here

HTML:

<ul> <!-- Each song on the album -->
    <li class="song-block"
        *ngFor='let song of songsToDisplay'
        (click)="getSong(song)"
        (mouseenter)="hoverStateIn()"
        (mouseleave)="hoverStateOut()">
      <div class="song-card"
           (click)="addPlay(song)">
        <p *ngIf="!song.isPlaying"
            class="song-number">{{song.tracknumber}}</p>
        <i *ngIf="song.isPlaying" class="fa fa-play"></i>
        <p class="song-name">{{song.name}}</p>
        <p class="song-length">{{song.length}}</p>
        <div class="thumbs"
             *ngIf="hover"> <!-- Thumbs section -->
          <i class="fa fa-thumbs-up"></i>
          <i class="fa fa-thumbs-down"></i>
        </div>.....
     </ul>

TypeScript:

hover: boolean = false;

  hoverStateIn(){
    this.hover = true
  }

  hoverStateOut(){
    this.hover = false;
  }
like image 706
Eddie Taliaferro Avatar asked Mar 07 '18 05:03

Eddie Taliaferro


2 Answers

You can simply set the hover boolean value to individual item of the *ngFor.

<ul>
  <!-- Each song on the album -->
  <li class="song-block"
      *ngFor='let song of songsToDisplay'
      (click)="getSong(song)"
      (mouseenter)="song.hover=true"
      (mouseleave)="song.hover=false">
    <div class="song-card"
         (click)="addPlay(song)">
      <p *ngIf="!song.isPlaying"
         class="song-number">{{song.tracknumber}}</p>
      <i *ngIf="song.isPlaying" class="fa fa-play"></i>
      <p class="song-name">{{song.name}}</p>
      <p class="song-length">{{song.length}}</p>
      <div class="thumbs"
           *ngIf="song.hover">
        <!-- Thumbs section -->
        <i class="fa fa-thumbs-up"></i>
        <i class="fa fa-thumbs-down"></i>
      </div>.....
</ul>
like image 161
Ali Shahzad Avatar answered Nov 17 '22 10:11

Ali Shahzad


A bit late, but hope it would be useful.

Using template reference variable is a quick simple way.

<div #hoverField></div>

<li (mouseenter)="hoverField.style.display = 'block';
    (mouseleave)="hoverField.style.display = 'none';>
</li>

Full code like this:

<ul> <!-- Each song on the album -->
    <li class="song-block"
        *ngFor='let song of songsToDisplay'
        (click)="getSong(song)"
        (mouseenter)="hoverField.style.display = 'block';"
        (mouseleave)="hoverField.style.display = 'none';">
      <div class="song-card"
           (click)="addPlay(song)">
        <p *ngIf="!song.isPlaying"
            class="song-number">{{song.tracknumber}}</p>
        <i *ngIf="song.isPlaying" class="fa fa-play"></i>
        <p class="song-name">{{song.name}}</p>
        <p class="song-length">{{song.length}}</p>
        <div class="thumbs"
             #hoverField> <!-- Thumbs section -->
          <i class="fa fa-thumbs-up"></i>
          <i class="fa fa-thumbs-down"></i>
      </div>.....
 </ul>
like image 3
KSY Avatar answered Nov 17 '22 12:11

KSY