angularjs實現導航菜單切換高亮


<ul>
      <li ng-repeat="(index, item) in headerList">
           <a ui-sref="{{item.href}}" 
               ng-class="{selected: selectedIndex === index}" 
               ng-click="changeIndex(index)">
                 <span>{{item.name}}</span>
           </a>
       </li>
</ul>
controller('headerCtrl', function($scope, $location, $state) {
        // 刷新時修正 selectedIndex
        var path = $location.$$path.split('/')[1];
        switch(path) {
            case '':
                $scope.selectedIndex = 0;
                break;
            case 'book':
                $scope.selectedIndex = 1;
                break;
            case 'borrow':
                $scope.selectedIndex = 2;
                break;
            case 'reader':
                $scope.selectedIndex = 3;
                break;
        }
        // 這里的href指的是ui-router的state的name
        $scope.headerList = [{
                href: 'home',
                name: '首頁'
            },
            {
                href: 'book',
                name: '圖書管理'
            },
            {
                href: 'borrow',
                name: '借還管理'
            },
            {
                href: 'reader',
                name: '讀者管理'
            }
        ];
        $scope.changeIndex = function(index) {
            $scope.selectedIndex = index;
        };
    });

效果界面:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM