用angularjs创建水平菜单

时间:2016-11-16 16:43:18

标签: angularjs sidebar

我想创建一个像this这样的菜单,但不使用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>

问题是我没有找到一种方法将参数传递给这个函数showChilds(当前的li)我尝试使用id但是它不起作用:

                    <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> 

我的控制器只包含此功能

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

我不想再使用$ scope.items

1 个答案:

答案 0 :(得分:1)

我已更新您的HTML。如果您没有使用ng-repeat,则可以使用ng-init初始化一个变量,并在ng-click上切换该变量以显示/隐藏子菜单。您更新的示例为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>