Using AngularJS to attach mouseover and mouseleave events to a specific child element within an li tag

I'm having trouble figuring out how to add mouse over/leave events to a span tag (child) of li.

Here is my HTML file:

<div ng-controller="SliderCtrl">
    <div class="left-panel">
    ...
    </div>
    <div class="right-panel">
        <gh-visualization val="data"></gh-visualization>
        <div class="chart">             
            <svg class="chart"></svg>
        </div>
        <div class="table">
            <ul>
                <li ng-repeat="chart in charts">
                    <div class="mark" style="background-color:{{colors[$index]}}"></div>
                    <div class="asset"><span ng-mouseenter="showTooltip({{$index}})" hover-element>{{chart.asset_class}}</span></div>
                    <div class="investment">{{chart.investment_fund}}</div>
                    <div class="weight">{{chart.weight}}</div>              
                </li>
            </ul>
        </div>
    </div>
</div>

This is my JS file:

var app = angular.module('MyApp', ["ngResource"]);
app.factory("Chart", function($resource) {
  ...
});

app.controller('SliderCtrl', function($scope, Chart) {
  ...
  $scope.showTooltip = function(index) {
    $scope.temp = index;
  }  
});

app.directive('ghVisualization', function () {

  return {
    restrict: 'AE',
    scope: {
      val: '=',
      charts: '='
    },
    link: function (scope, element, attrs) {   
        ...
      scope.showTooltip = function(index) {
        var temp = index;
      }
    }
  }
});

app.directive('hoverElement', function () {    
  return {
    restrict: 'AE',
    scope: {
    },
    link: function (scope, element, attrs) {     
      element.bind("mouseover", function(e){    
           element.addClass('unfill').removeClass('filled');
      });
      element.bind("mouseout", function(e){
           element.addClass('filled').removeClass('unfill');
      });
    }
  }
});

Let me provide some context about my code:

The SliderCtrl controller and ghVisualization directive are already set up. What I'm attempting to do is apply mouse over/leave events to the nested span tag within li for specific actions.

Initially, I added ng-mouseenter="showTooltip({{$index}})" to the span and defined the showTooltip function in the controller (but it wasn't called). I also tried implementing it within the directive, but with no success.

To address this issue, I created a new directive named hoverElement and linked it to the span, yet it still didn't work as intended.

If anyone can point out what might be wrong with my approach and provide guidance on how to properly execute this task in AngularJS, I would greatly appreciate it.

Thank you in advance!

Answer №1

In order to pass a scope property as a parameter to an event-handling function (attached with ng-_eventtype_), there is no need for using interpolation ({{ }}).

ng-mouseenter="showTooltip($index)"

The hoverElement directive appears to be working fine based on this example.

Answer №2

I believe the issue lies in how you have implemented ng-mouseenter

ng-mouseenter="showTooltip($index)"

It should be written like this, instead:

ng-mouseenter="showTooltip({{$index}})"

You can check out a demonstration on this website

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The readyState remains stuck at 1 without progressing further

Struggling to input data into an XML using javascript's open() function. The website remains stuck at readyState 1, Here is the Javascript code snippet: function addItem() { var name = document.getElementById('Iname').value; va ...

Avoiding POST spam in Node.js Express by utilizing busboy

I found guidance from this source: https://www.npmjs.com/package/busboy Concerns arise about potential server overload from malicious activity. Is there a way to prevent spamming by measuring the overall size of the POST body, including non-file data prio ...

What mistakes am I making in my usage of React hooks in this situation?

As part of my journey through the FullstackOpen course at the University of Helsinki, I am working on creating a simple Phonebook application. In troubleshooting my code, I've noticed that my 'filterBy' state is consistently one step behind, ...

Trouble with sending semicolons in Node.js MSSQL/MSNodesqlv8 database queries

Trying to create a simple API for interacting with a MSSQL v12 database using Nodejs. Successfully connected to the database with the mssql/msnodesqlv8 package, but encountering issues with parameterized queries. code: 'EREQUEST', number: 102 ...

How can I utilize a filter or pipe to populate product categories onto screens within Ionic 2?

I am considering creating an Ionic 2 app with 6 pages, but I'm unsure whether to utilize a Pipe or a Filter for the individual category pages and how to implement the necessary code. Each category page should be able to display products from the "app ...

Trigger a ModalPopupExtender within an UpdatePanel with the use of JavaScript

I'm looking to trigger a ModalPopup using JavaScript. After researching on StackOverflow, the common suggestion is to use the following code snippet: $('#inputAdd').live("click", function () { $('#addRow').show(); $find(&a ...

Challenge encountered when trying to store AngularJS response in a global variable

When making an HTTP call to the controller using angular.js, I am trying to save the response in a global variable like so: app.controller('dashBoardController', ['$scope', '$http', '$location', function ($scope ...

Using V-for in Vue.js to iterate over data sources

I am attempting to display all elements of an array, but currently can only show the first line due to [0]. I want to show all items in the array. <div class="description" v-for="item in sitePartVoice[0].part_attributes"> <small><strong> ...

What could be causing Protractor to execute each line of code without delay?

Why is Protractor executing each line of code immediately? I have a non-angular webpage that I need my selenium-based automation to interact with. I've written selenium webdriver-js code to accomplish this task. For example, after logging in, the use ...

What is the proper method for running a script using the Selenium JavascriptExecutor?

On my test.html page, I've included the following code snippet: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> </head> <body> ...

In Chrome, there is a conflict between the screen width when using `position: absolute` and `position: fixed` if there is vertical

I'm in the process of developing a website with a video banner set to fixed positioning in the background, and a div that has absolute positioning covering part of the video on the bottom half. However, I've encountered an issue - when I add this ...

What is the proper way to incorporate parentheses (either "(" or ")") on the JavaScript calculator's output screen?

This is the customized button I made for the parentheses. <div class="col"> <button class="parentheses rounded-pill btn-outline-primary btn btn-lg" id="keyButton" data-key="operator"&qt;()</but ...

This context does not contain the expected object "this"

I seem to be losing the reference to the "This" object within the helper class in this particular structure, and I'm not sure why. When looking at the getAll method, the this object is currently pointing to the object housed within the servicesDict a ...

Recovering antiquated submission script in JavaScript

Here is a form with input data: <form id = 'myform'> ... <td><input type="checkbox" name="supplier_aid" value="on" checked disabled >{$output.t_artikelnr}</td> <td><input type="checkbox" n ...

is there a way to modify the background color of a div element by comparing values in javascript?

Is there a way to dynamically update the background color of a div element within a table based on values stored in a json array from a database? ...

Managing code requiring document.title in Next.js with Static Site Generation (SSG)

Currently, I have 2 dynamic SSG pages located under /blog/[slug]. Within these pages, I am rendering a component using next/link. When I click on these links to navigate to another slug, I encounter an issue. The problem arises when I want to execute some ...

Activate the submission button only when all the necessary fields have been completed

I am currently working on a form that consists of 3 fields: email_from, email_subject, and an editor. The editor is built using draftjs. I am facing an issue with enabling the submit button only when all the fields are filled without any errors. Below is ...

Display a Vue.js div element based on conditions matching a specific variable value

Is it possible for Vue.js to display a div only when a defined variable is set to a specific value? Currently, v-show="variable" can be used to show the div if the variable is set. However, I would like to know if v-show="variable=5" can be implemented t ...

What steps do I need to follow to create a 3D shooting game using HTML5 Canvas?

I'm eager to create a 3D shooter game with HTML5 Canvas, focusing solely on shooting mechanics without any movement. Can anyone provide guidance on how to accomplish this? I've looked for tutorials online, but haven't come across any that m ...

Experiencing difficulties when attempting to show or hide elements using jQuery

I spent several hours trying to figure this out and couldn't get it right. Is it feasible to create a jQuery script that will perform the following action when a <span> element is clicked: Check if any of the <p> elements are current ...