ng-mousover to select ONLY the hovered element not all elements - using ng-repeat
NickName:NiBu Ask DateTime:2016-12-01T09:22:23

ng-mousover to select ONLY the hovered element not all elements - using ng-repeat

I need to add and remove a class to an element on mouseover. The method below adds and removes classes from all elements with classname .blogOverlay and .newOverlay.

I need it to add/remove the class ONLY on the element that is being hovered over.

JS:

$scope.showReadMore = function(){
  $('.blogOverlay').addClass("hidden");
  $('.newOverlay').removeClass('hidden');
}
$scope.hideReadmore = function(){
  $('.blogOverlay').removeClass("hidden");
  $('.newOverlay').addClass('hidden');
}

HTML:

  <div ng-if="!post.firstFeatured" class="col-sm-10 blog-content blogPreview" >
                            <a ng-click="goToPostDetail(post, $index)" >
                              <img class="img-responsive img-blog" ng-src="{{ post.fields.image.fields.file.url }}" width="100%" alt=""  />
                              <div class="blogOverlay" ng-mouseover="showReadMore()" ng-mouseleave="hideReadmore()">
                                  <h2>{{ post.fields.title }}</h2>
                              </div>

                              <div class="newOverlay hidden" ng-mouseover="showReadMore()" ng-mouseleave="hideReadmore()">
                                  <h2>{{ post.fields.title }}</h2>
                                  <h3>{{post.fields.date}}</h3>
                                  <a class="btn btn-primary readmore" ng-click="goToPostDetail(post, $index)">Read More</a>
                              </div>
                            </a>
                        </div>

Copyright Notice:Content Author:「NiBu」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40901119/ng-mousover-to-select-only-the-hovered-element-not-all-elements-using-ng-repea

Answers
lealceldeiro 2016-12-01T01:54:36

There is no need to use jquery. Just use ng-class and add a condition to show or hide that class to your post. Se snipped how content is shown or hidden with the hidden class according to property post.readMore in the controller\n\n\r\n\r\nangular.module('myapp', [])\r\n .controller('foo', function($scope) {\r\n\r\n $scope.post = {\r\n readMore: true,\r\n fields: {\r\n title: 'The post title',\r\n date: new Date()\r\n }\r\n }\r\n\r\n $scope.showReadMore = function(post) {\r\n post.readMore = true;\r\n }\r\n $scope.hideReadmore = function(post) {\r\n post.readMore = false;\r\n }\r\n\r\n\r\n });\r\n<!DOCTYPE html>\r\n<html>\r\n\r\n<head>\r\n <script src=\"https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js\"></script>\r\n\r\n <style type=\"text/css\">\r\n .hidden {\r\n display: none;\r\n }\r\n </style>\r\n\r\n</head>\r\n\r\n<body ng-app=\"myapp\">\r\n <div ng-controller=\"foo\">\r\n\r\n <div ng-if=\"!post.firstFeatured\" class=\"col-sm-10 blog-content blogPreview\" style=\"max-width: 400px\">\r\n <a ng-click=\"goToPostDetail(post, $index)\">\r\n <img class=\"img-responsive img-blog\" ng-src=\"{{ post.fields.image.fields.file.url }}\" width=\"100%\" alt=\"\" />\r\n\r\n <div class=\"blogOverlay\" ng-class=\"{'hidden' : !post.readMore}\" ng-mouseover=\"showReadMore(post)\" ng-mouseleave=\"hideReadmore(post)\">\r\n <h2>{{ post.fields.title }}</h2>\r\n </div>\r\n\r\n <div class=\"newOverlay\" ng-class=\"{'hidden' : post.readMore}\" ng-mouseleave=\"showReadMore(post)\" ng-mouseover=\"hideReadmore(post)\">\r\n <h2>{{ post.fields.title }}</h2>\r\n <h3>{{post.fields.date}}</h3>\r\n <a class=\"btn btn-primary readmore\" ng-click=\"goToPostDetail(post, $index)\">Read More</a>\r\n </div>\r\n\r\n </a>\r\n </div>\r\n\r\n </div>\r\n\r\n\r\n</body>\r\n\r\n</html>",


Rohit Shedage 2016-12-01T01:33:05

give class name something like class=\"blogOverlay_{{$index}}\" and then pass $index to JavaScript like showReadMore($index)\n\nThis way Your class names are unique and they will change for only index you want to change",


More about “ng-mousover to select ONLY the hovered element not all elements - using ng-repeat” related questions

ng-mousover to select ONLY the hovered element not all elements - using ng-repeat

I need to add and remove a class to an element on mouseover. The method below adds and removes classes from all elements with classname .blogOverlay and .newOverlay. I need it to add/remove the c...

Show Detail

Select all elements inside sibling elements except the hovered element only on a hover

I want to select the anchor elements in all the .col-md-4 elements in my .blog-grid element when I hover over one of the anchor elements. I also don't want to select the element I am hovering over....

Show Detail

Animation only for hovered element

Trying to do some trick with my post information. When I hover mouse on one element - all elements became active. How can I made this animation only to hovered element? Tried it: $('.post-pic-hol...

Show Detail

How to using ng-repeat select all element, but without some class

How can I use ng-repeat to select all elements but exclude all of them which have a class bookmaker-disabled. Screen shot I am using: element.all(by.repeater('bookmaker in vm.bookmakers')) What ...

Show Detail

Get all not hovered elements and do something with them

My problem is probably quite simple, but somehow I can’t get it fixed. In a group of objects with one object hovered over, I’d like the hovered object to remain unchanged, but all others are suppos...

Show Detail

How to select on click the hovered element not the hover element?

I am using jquery to drag and drop some element and for some too small element i display hover a div with info content. My problem is when i want to drag this element i select the hover div not the

Show Detail

How to select element under currently hovered element

I have a div containing content that I want to show a title pop-up when hovered on. I also want the entire div area to be clickable. To make the entire div clickable I have created a single anchor ...

Show Detail

Target child element of hovered element (animejs)

I have these elements: &lt;a class=&quot;parent&quot;&gt; &lt;span class=&quot;child&quot;&gt;&lt;/span&gt; &lt;/a&gt; I want to target child element only when parent is hover. So, I t

Show Detail

Trigger bar/pie graph highlight when hovering over an external element [chart.js / chart js] [AngularJS] [angular-chart.js]

I am using angular-chart.js to create a dashboard. Instead of using the built in legend within the canvas I created my own and am using ng-repeat to dynamically create the labels with matching colo...

Show Detail

Jquery only affect one element in a class being hovered

Not sure if this is possible. I want to select all elements with a class name and only affect the one element being hovered at that time and not the whole class. i can't use ids since they are a lo...

Show Detail