【问题标题】:ng-class to highlight active menu item based on ng-repeat. AngularJSng-class 基于 ng-repeat 突出显示活动菜单项。 AngularJS
【发布时间】:2013-11-13 00:57:36
【问题描述】:

我有一个基于以下示例的菜单:

 <nav  data-ng-controller="menuContrl" class="menuItem">
     <a  data-ng-class='{active:isActive("/{{item.path}}")}' data-ng-repeat="item in menu" href="#/{{item.path}}">
         <span>{{item.title}}</span>
     </a>
 </nav>

item 是一个对象,包含菜单项信息。以下是指令和控制器的 JavaScript 代码:

var app = angular.module("coolApp",[]);

function menuContrl($scope,$location){
    $scope.menu=menu;
    $scope.isActive = function(path){
        return ($location.path()==path)
    } 
}

问题是ng-class 在页面渲染期间仅将class 设置为active 一次,但是当您单击菜单项时,什么也没有发生。我想这是因为菜单本身没有重新加载,我只是在&lt;div&gt; 中更改数据。那么如何在不重新加载整个页面的情况下使其工作呢?

【问题讨论】:

  • 你的意思是这样的吗? http://jsfiddle.net/codef0rmer/uDPHL/
  • 是的,如果您手动添加每个菜单项,这将起作用。当您使用 ng-repeat 生成菜单项时,它会按预期停止工作。
  • 你可以使用ui-sref-active see here

标签: javascript angularjs


【解决方案1】:

试试这个:- http://jsfiddle.net/uDPHL/146/

旧版angular js存在此问题Reference,升级到angular js 1.2.0版本后问题得到解决。

JS:-

var navList = angular.module('navList', []);    

navList.controller('navCtrl', ['$scope', '$location', function ($scope, $location) {

    $scope.navLinks = [{
        Title: 'home',
        LinkText: 'Home',
    }, {
        Title: 'about',
        LinkText: 'About Us'
    }, {
        Title: 'contact',
        LinkText: 'Contact Us'
    }];

    $scope.navClass = function (page) {
        var currentRoute = $location.path().substring(1) || 'home';
        return page === currentRoute ? 'active' : '';
    };   

}]);

HTML:-

<div class="well sidebar-nav" ng-app="navList">
    <ul class="nav nav-list" ng-controller="navCtrl">
        <li ng-repeat="navLink in navLinks" ng-class="navClass('{{navLink.Title}}')"> <a href='#/{{navLink.Title}}'>{{navLink.LinkText}}</a> 
        </li>
    </ul>
</div>

【讨论】:

  • @Alexus 这能回答你的问题吗?
【解决方案2】:

我发现如果你去的话会容易一些:

<li ng-repeat="i in mainMenu" ng-class="{'active':i.path == path}">

然后你可以在你的控制器中更改 $scope.path。

【讨论】:

    【解决方案3】:

    //app.run
    App.run(["$rootScope", "$state", "$stateParams",function ($rootScope, $state, $stateParams) {
            // Set reference to access them from any scope
            $rootScope.$route = $state;
    }]);
            
    
    // app.config
            $stateProvider
                .state('admin', {
                    url: '/admin/',
                    templateUrl: 'app/admin/admin.html',
                    controller: 'AdminController',
                })
                .state('admin.dashboard', {
                    url: 'dashboard',
                    title: 'Dashboard',
                    activetab: 'dashboard',
                    controller: 'DashboardController',
                    templateUrl: 'app/admin/dashboard.html',
    
                })
                .state('admin.view1', {
                    url: 'view1',
                    title: 'view1',
                    activetab: 'view1',
                    controller: 'view1Controller',
                    templateUrl: 'app/admin/view1.html',
                   
                })
                .state('admin.view2', {
                    url: 'view2',
                    title: 'view2',
                    activetab: 'view2',
                    controller: 'View2Controller',
                    templateUrl: 'app/admin/view2.html',
                });
    <li class="h4" ng-class="{'active': $route.current.activetab == 'dashboard'}">
                            <a>Dashboard</a>
                        </li>
                        <li class="h4" ng-class="{'active': $route.current.activetab == 'view1'}">
                            <a>view1</a>
                        </li>
                        <li class="h4" ng-class="{'active': $route.current.activetab == 'view2'}">
                            <a active-link="active">view2</a>
                        </li>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多