0

I want to create a menu like this but without using ng-repeat:

<ul class="sidebar-navi">
    <li ng-repeat="item in items" ng-click="showChilds(item)">
        <a href="" class="sidebar-nav-menu"><i class="fa fa-angle-left sidebar-nav-indicator sidebar-nav-mini-hide"></i><i class="fa fa-cogs sidebar-nav-icon"></i>{{item.name}}</a>
        <ul>
            <li ng-show="item.active">
                <span>First</span>
            </li>
            <li ng-show="item.active">
                <span>Second</span>
            </li>
        </ul>
    </li>
</ul>

the problem is that I didn't find a way to pass parameter to this function showChilds (the current li) I tried to work with id but it didn't work:

                    <ul class="sidebar-navi">
                        <li id="first" ng-click="showChilds(first)">
                            <a href="" class="sidebar-nav-menu"><i class="fa fa-angle-left sidebar-nav-indicator sidebar-nav-mini-hide"></i><i class="fa fa-cogs sidebar-nav-icon"></i>1</a>
                            <ul>
                                <li ng-show="first.active">
                                    <span>First</span>
                                </li>
                                <li ng-show="first.active">
                                    <span>Second</span>
                                </li>
                            </ul>
                        </li>
                        <li id="second" ng-click="showChilds(second)">
                            <a href="" class="sidebar-nav-menu"><i class="fa fa-angle-left sidebar-nav-indicator sidebar-nav-mini-hide"></i><i class="fa fa-cogs sidebar-nav-icon"></i>2</a>
                            <ul>
                                <li ng-show="second.active">
                                    <span>First</span>
                                </li>
                                <li ng-show="second.active">
                                    <span>Second</span>
                                </li>
                            </ul>
                        </li>
                    </ul> 

and my controller will contain only this function

  $scope.showChilds = function (item) {
    item.active = !item.active
  }

I don't want to use $scope.items anymore

3
  • where is your code without ng-repeat? Commented Nov 16, 2016 at 16:45
  • where do you set the value of item? It's a user input? Commented Nov 16, 2016 at 16:49
  • please check the update Commented Nov 16, 2016 at 16:51

1 Answer 1

1

I have updated your HTML. If you are not using ng-repeat then you can use ng-init to initialize one variable and toggle that variable on ng-click to show/hide sub menus. Your updated sample is here

 <ul class="sidebar-navi">
                    <li id="first" ng-click="first = !first" ng-init="first = false">
                        <a href="" class="sidebar-nav-menu"><i class="fa fa-angle-left sidebar-nav-indicator sidebar-nav-mini-hide"></i><i class="fa fa-cogs sidebar-nav-icon"></i>1</a>
                        <ul>
                            <li ng-show="first">
                                <span>First</span>
                            </li>
                            <li ng-show="first">
                                <span>Second</span>
                            </li>
                        </ul>
                    </li>
                    <li id="second" ng-click="second = !second" ng-init="second = false">
                        <a href="" class="sidebar-nav-menu"><i class="fa fa-angle-left sidebar-nav-indicator sidebar-nav-mini-hide"></i><i class="fa fa-cogs sidebar-nav-icon"></i>2</a>
                        <ul>
                            <li ng-show="second">
                                <span>First</span>
                            </li>
                            <li ng-show="second">
                                <span>Second</span>
                            </li>
                        </ul>
                    </li>
                </ul> 
Sign up to request clarification or add additional context in comments.

1 Comment

I don't want to use items list anymore I want to use only html please check the update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.