Using ngClass for hovering effect in Angular

If I have the following code snippet:

 <div 
     class="italic" 
     ng-class="{red: hover}" 
     ng-mouseenter="hover = true"
     ng-mouseleave="hover = false">
         Test 1 2 3.
    </div>
     <div 
     class="italic" 
     ng-class="{red: hover}" 
     ng-mouseenter="hover = true"
     ng-mouseleave="hover = false">
         Test 1 2 3.
    </div>  

I am trying to change the color to red when a user hovers over the div. However, currently both divs turn red when hovered over.
[Fiddle]1 for experimenting with the code

Answer â„–1

Utilize the directive provided below:

.directive('hoverEffect', function () {
    return {
        restrict: 'A',
        scope: {
            hoverClass: '@'
        },
        link: function (scope, element) {
            element.on('mouseenter', function() {
                element.addClass(scope.hoverClass);
            });
            element.on('mouseleave', function() {
                element.removeClass(scope.hoverClass);
            });
        }
    };
});

Include this snippet of HTML:

<div class="bold" hover-effect="blue" >
     Example Text.
</div>  

For more information, visit this link

Answer â„–2

<div class="italic" ng-repeat="element in elements" ng-mouseover="highlightSelection($index)" 
ng-mouseleave="removeHighlight($index)" ng-class="{blue: $index == {{selection}}}">


Controller:
$scope.highlightSelection = function(idx) {
     $scope.selection = idx;
     }

$scope.removeHighlight = function(idx) {
     $scope.selection = '';
     }

Answer â„–3

No special directive or function is required for this task. All you need to do is create a distinct variable for each individual div. This could be named as hover1 and hover2, hovers.div1 and hovers.div2, or even hover[0] and hover[1]. If both divs are linked to the same variable, any action taken will affect both divs simultaneously.

Check out this example: http://jsfiddle.net/pthfV/841/

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

Transferring scope between pages without the need for an angular service definition

Looking to open a new JSP page while passing the $scope in order to utilize an array response generated in the initial page. Example from test.js file: (function() { 'use strict'; angular .module('test', []) .control ...

The unresolvable IE7 Z-Index dilemma caused by a bug within nested elements!

Take a look. I'm struggling to get the .menu ul to display properly, it's frustrating! It shows up in ie6, ie8, safari, and ff, but not ie7! I've tried everything. Any suggestions? (I'm almost at xhtml strict validation too). ...

refresh the route without having to reload the controller

I have a specific detail page dedicated to each car ID, on this page, there is a gallery of photos fetched from the service. Users can click on these photos to view them in full size. I am looking to create a shareable URL that includes the selected car, ...

Database stores line breaks in a structured format

As I work on creating a posts section for my readers, I have encountered an issue. When a user adds a new post, it gets saved into the database in this format: Some say the world will end in fire,<br /> Some say in ice.<br /> From what I’ve ...

PDF links not loading properly in Chrome's PDF Viewer, causing them to become stuck

While working on a website we are developing, we encountered an issue with PDF download links getting stuck while loading in Chrome's built-in PDF viewer. The links work fine in other browsers and can be triggered for download. Right-clicking and sele ...

The art of CSS Absolute Positioning and the Science of Body Sc

I'm trying to position an absolute div in the bottom right corner of a page. .absolute{ position: absolute; right:0; bottom: 0; } While this code works, I've noticed that when scrolling the page, the right/bottom position is relative t ...

What steps can I take to guarantee that my grid always accommodates its child without being too small?

My current setup involves utilizing a grid layout to arrange elements in the z-direction. However, I have encountered an issue where a child element extends beyond the boundaries of the grid element: #z-stack { display: grid; border: 5px solid ...

What are the best ways to utilize dynamic CSS on a smartphone browser for optimal performance?

Struggling to optimize my web application for mobile devices, I've encountered challenges with consistent display across different browsers. My idea is to utilize device capability detection to adjust widths and font sizes dynamically based on screen ...

Implementing Dynamic Parent Node Manipulation with Button Clicks in JavaScript

I am currently working on dynamically creating an input field using the append child method along with add and delete buttons to form a tree-like structure. The goal is to be able to create child nodes with add and delete buttons upon clicking the add butt ...

Differences between THIS and angular's $SCOPE.$ONIn JavaScript,

After working with Angular 1.x for a couple of years, I decided to experiment with new approaches to enhance my understanding of Angular and JavaScript. Previously, I believed that the "vm" declaration in the JavaScript controller was directly linked to th ...

Retrieving a directive's value within a controller

Although similar questions have been asked numerous times before, none of the solutions seem to work for me. I am new to working with directives and it seems like my lack of experience is hindering my ability to resolve this issue. Within my directive, I ...

Ideas for concealing a header on a particular page in WordPress

Attempting to conceal the header on a specific WordPress page has been quite the challenge for me. I am aware that CSS can come in handy for this purpose. The dashboard displays the page ID as: wp-admin/post.php?post=31221&action=edit This particula ...

The controller is being utilized by two different directives

Consider the following scenario: function directive() { return { template: '{{foo.name}}', controller: ctrl, controllerAs: 'foo' } } function ctrl($attrs) { this.name = $attrs.name; } If you have t ...

What is the best way to create a search bar that overlaps with a filter button?

Desired Ui Looking to create a search bar with a button positioned to the right of it, partially overlapping a banner. Above these elements is a title; however, one issue I'm encountering is that the title covers the search bar when the page size is r ...

Tips for creating an onChange function in an input field using jQuery

I am looking to dynamically create inputs where each input has an `onChange` function with a variable. Here is my code: var distinct_inputs = 0; $('.icon1').click( function(){ distinct_inputs = distinct_inputs + 1 ; $('#insert-file&apo ...

Advantages of using individual CSS files for components in React.js

As someone diving into the world of Web Development, I am currently honing my skills in React.js. I have grasped the concept of creating Components in React and applying styles to them. I'm curious, would separating CSS files for each React Component ...

Completing a form and reloading the page without including the reload in the browsing history

I am currently developing a compact web application using Flask, and I have encountered a challenging issue with navigating back to the previous page. Here are some approaches I have experimented with: Avoiding navigation away from the page - content ...

Create a DIV element that completely fills the vertical space available

My webpage is currently looking quite empty, with only one DIV element present: <div style="height: 20%; min-height: 10px; max-height: 100px; width: 100%; background-color: blue;"></div> I' ...

Is There a Name Clash Issue with Dependency Injection in AngularJS?

Let's say I have two modules, finance2 and finance3, both of which define a service called currencyConverter. If I specify that my main module only depends on finance2, I can inject the service like this: angular.module('invoice2', [' ...

A guide on incorporating CSS animations triggered by reaching specific media-query breakpoints

In order to make our website responsive to mobile devices, I am utilizing CSS media queries to switch between a header bar and a hamburger menu. I had the idea of adding a subtle animation when desktop users resize their browser window beyond the defined ...