2

I was trying to show and hide an element child, so when I hover over a <li> element, I set up a ng-show with value false for all the elements. If I try to make the value true to show, it shows me all the child elements. I just want to display for each element hovered its child:

HTML CODE:

<ul ng-init="show=false">
    <li>
        <a class="list-group-menu" href="#"><div class="icon-user"></div></a>
        <div ng-show="show"> Profile </div>
    </li>
    <li>
        <a class="list-group-menu" href="#"><div class="icon-scope"></div></a>
        <div ng-show="show"> Scope </div>
    </li>
    <li>
        <a class="list-group-menu" href="#"><div class="icon-job"></div></a>
        <div ng-show="show"> Job </div>
    </li>

    <li>
        <a class="list-group-menu" href="#"><div class="icon-login"></div>
            <div ng-show="show"> Login </div>
        </a>
    </li>
    <li>
    <a class="list-group-menu" href="#"><div class="icon-register"></div></a>
     <div ng-show="show"> Register </div>
    </li>
</ul>

JQUERY CODE:

$('ul li').mouseenter(function() {

   $(this).children('div').show();

});

$('ul li').mouseleave(function() {

   $(this).children('div').hide();

});

3 Answers 3

5

This is not an angular way. You should do like this.

JS

angular.module("firstApp").controller("myCtrl",function($scope) {
    $scope.allMenu = ["Profile","Scope","Job","Login","Register"];
});

HTML

<ul ng-controller="myCtrl">
    <li ng-repeat="menuName in allMenu" ng-mouseenter="show=true" ng-mouseleave="show=false">
        <a class="list-group-menu" href="#"><div class="icon-user"></div></a>
        <div ng-show="show"> {{menuName}} </div>
    </li>
</ul>
Sign up to request clarification or add additional context in comments.

3 Comments

Oh, Sorry i'm really new to angularJs this solution make sense, but how can tou see i have different class inside the <div> elements, i have just to create an object similar like so? var allMenu = { [ "name": "profile", "icon":"icon-user"] }
Fabrizio; yes, try it. Though more likely allMenu = [{name:'test', custom_class:'specific_class'}, {..}], then you can use ng-class='{custom_class}'
As Erti-Chris said, you can use ng-class. Read this docs.angularjs.org/api/ng/directive/ngClass
4

You don't need any javascript here at all. This is pure CSS task:

.menu .list-group-menu + div {
    display: none;
}
.menu .list-group-menu:hover + div {
    display: block;
}

for HTML:

<ul class="menu">
    <li>
        <a class="list-group-menu" href="#">
            <div class="icon icon-user">User</div>
        </a>
        <div>Profile</div>
    </li>
    <li>
        <a class="list-group-menu" href="#">
            <div class="icon icon-scope">Scope</div>
        </a>
        <div>Scope</div>
    </li>
    ...

2 Comments

That's sound good, thank you for this trick, but this one was just a question for understand how angularJs do this job. :)
Sure. If you don't want to have an array of items defined in controller then you would need to write a directive.
1

You are binding all of the elements to the "show" variable, so when you flip it to true, all elements will show. If you want to use ng-show for that purpose, use an array of objects containing all of the data for the template and an ng-repeat. That will not only solve your problem but also DRY your template. I would slso suggest you look into $element so that you can stick to the angular environment and syntax instead of also having to use jquery.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.