0

I'm generating some div tags with the ng-repeat directive. What I'd like is that when the user hovers over one of the div tags, only that div tag will change its background. However, as it stands, when the user hovers over the tag, all the ng-repeat generated elements have the property applied to them.

Here's my code for the element:

    <div class="col-xs-12">
        <div class="row">
            <div ng-repeat="verb in verbArray | filter: filterText" class="col-xs-12 col-sm-3" 
             ng-mouseenter="changeColor(true)" ng-mouseleave="changeColor(false)"
             ng-style="highlightColor">
                {{verb.name}}<img src="{{verb.presentImage}}"/><img src="{{verb.pastImage}}"/>
            </div>
        </div>
    </div>

Here is the changeColor function in the controller

    $scope.changeColor = function(theBool) {
        if(theBool === true) {
            $scope.highlightColor = {'background-color': 'green'};
        } else if (theBool === false) {
            $scope.highlightColor = {'background-color': 'white'}; //or, whatever the original color is
        }
    };

How can I make it so that the only thing that has the changeColor function applied is the element that's being hovered over?

Thank You for the help

Edit: If I did it with CSS, would it work like this:

            <div ng-repeat="verb in verbArray | filter: filterText" class="col-xs-12 col-sm-3" 
             ng-mouseenter="changeColor(true)" ng-mouseleave="changeColor(false)"
             ng-style="highlightColor" class="classOne">
                {{verb.name}}<img src="{{verb.presentImage}}"/><img src="{{verb.pastImage}}"/>
            </div>
        </div>

CSS file

.classOne:hover {
 background-color: green;
}
4
  • How about adding a custom attribute and populating it via $index. Then target the one you wish tp Commented Apr 16, 2017 at 20:53
  • hover, please read Commented Apr 16, 2017 at 20:56
  • @Tushar. I'm sorry. I'm very new to AngularJS and Javascript in general. I don't really know the ins and outs of the language yet. How do I make a custom attribute? How do I use this $index service. Do I need to inject it into my controller? Commented Apr 16, 2017 at 21:02
  • @Tushar. I got it to work through CSS. However, I'm curious about the method you mentioned. I think it may come in handy in the future. Could you please let me know where I might find more information about this topic? Thanks Commented Apr 16, 2017 at 21:46

1 Answer 1

2

You can use CSS to obtain this effect with the :hover selector (https://www.w3schools.com/cssref/sel_hover.asp)

like this:

div:hover {
    background-color:green;
}

doing this through CSS is generally faster than using Angular to do it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but how do select a div to apply it to. Should I assign the div with the ng-repeat in a CSS class and apply it to that class?
Yes, that is a great idea, you could name it something like .listitem and apply the CSS rule on .listitem:hover

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.