S
S
sanex33392015-09-24 15:11:59
Angular
sanex3339, 2015-09-24 15:11:59

How to properly organize ng-show and ng-hide in Angular?

There is a template

<ul class="menu">
                    <li class="menuItem" ng-repeat="menuItem in menu">
                        <a href="{{ menuItem.route }}" ng-click="showSubmenu = !showSubmenu">{{ menuItem.text }}</a>
                        <ul class="subMenu" ng-show="showSubmenu">
                            <li class="subMenuItem" ng-repeat="subMenuItem in menuItem.subMenu">
                                <a href="{{ menuItem.route }}{{ subMenuItem.route }}">{{ subMenuItem.text }}</a>
                            </li>
                        </ul>
                    </li>
                </ul>

Menu with submenus - ul>li>ul>li
Problem: when clicking on any 1 element of the main menu, its submenu is collapsed/expanded, the remaining submenus of other elements of the main menu retain their current expanded/collapsed state.
It is necessary that when you click on any element of the main menu, all other expanded submenu elements are collapsed, and the current one is expanded accordingly. How to do it right ?
I tried to select all submenus through angular.element(document.querySelector('.subMenu')) - returns only the submenu of the current menu element, as I understand it because of the separate scope?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2015-09-24
@sanex3339

make showSubmenu an array and change the value of each element.
Then on onclick you can run through the array and close the extra li
Something like showSubmenu[menuItem.id]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question