user3398172 user3398172 - 4 months ago 99x
jQuery Question

ng-mouseover and leave to toggle item using mouse in angularjs


<ul ng-repeat="task in tasks">
<li ng-mouseover="hoverIn()" ng-mouseleave="hoverOut()">{{}}</li>
<span ng-show="hoverEdit"><a>Edit</a></span>


$scope.hoverIn = function(){
$scope.hoverEdit = true;

$scope.hoverOut = function(){
$scope.hoverEdit = false;

The code is ridiculous because I think it's too much. I think it can be simplified. Anyway the result toggle all the item once it's hovered. I've jQuery background, so I have no idea how to make single item work in


Angular solution

You can fix it like this:

$scope.hoverIn = function(){
    this.hoverEdit = true;

$scope.hoverOut = function(){
    this.hoverEdit = false;

Inside of ngMouseover (and similar) functions context is a current item scope, so this refers to the current child scope.

Also you need to put ngRepeat on li:

    <li ng-repeat="task in tasks" ng-mouseover="hoverIn()" ng-mouseleave="hoverOut()">
        <span ng-show="hoverEdit">


CSS solution

However, when possible try to do such things with CSS only, this would be the optimal solution and no JS required:

ul li span {display: none;}
ul li:hover span {display: inline;}