如何用Angular JS设置引导导航栏活动类

How to set bootstrap navbar active class with Angular JS?

本文关键字:导航 活动 何用 Angular JS 设置      更新时间:2023-09-26

如果我在bootstrap中有一个导航栏,上面有

Home | About | Contact

当每个菜单项处于活动状态时,如何设置活动类?也就是说,当角路由在

时,如何设置class="active"
  1. #/
  2. 关于
  3. #/about
  4. 联系人界面
  5. #/contact

一种非常优雅的方法是使用ng-controller在ng-view之外运行单个控制器:

<div class="collapse navbar-collapse" ng-controller="HeaderController">
    <ul class="nav navbar-nav">
        <li ng-class="{ active: isActive('/')}"><a href="/">Home</a></li>
        <li ng-class="{ active: isActive('/dogs')}"><a href="/dogs">Dogs</a></li>
        <li ng-class="{ active: isActive('/cats')}"><a href="/cats">Cats</a></li>
    </ul>
</div>
<div ng-view></div>

并包含在controllers.js中:

function HeaderController($scope, $location) 
{ 
    $scope.isActive = function (viewLocation) { 
        return viewLocation === $location.path();
    };
}

我刚刚写了一个指令来处理这个,所以你可以简单地将属性bs-active-link添加到父<ul>元素中,并且任何时候路由改变,它都会找到匹配的链接,并将active类添加到相应的<li>中。

你可以在这里看到它的作用:http://jsfiddle.net/8mcedv3b/

HTML例子:

<ul class="nav navbar-nav" bs-active-link>
  <li><a href="/home">Home</a></li>
  <li><a href="/contact">Contact</a></li>
</ul>
Javascript:

angular.module('appName')
.directive('bsActiveLink', ['$location', function ($location) {
return {
    restrict: 'A', //use as attribute 
    replace: false,
    link: function (scope, elem) {
        //after the route has changed
        scope.$on("$routeChangeSuccess", function () {
            var hrefs = ['/#' + $location.path(),
                         '#' + $location.path(), //html5: false
                         $location.path()]; //html5: true
            angular.forEach(elem.find('a'), function (a) {
                a = angular.element(a);
                if (-1 !== hrefs.indexOf(a.attr('href'))) {
                    a.parent().addClass('active');
                } else {
                    a.parent().removeClass('active');   
                };
            });     
        });
    }
}
}]);

你可以看看AngularStrap,导航条指令似乎是你正在寻找的:

https://github.com/mgcrea/angular-strap/blob/master/src/navbar/navbar.js

.directive('bsNavbar', function($location) {
  'use strict';
  return {
    restrict: 'A',
    link: function postLink(scope, element, attrs, controller) {
      // Watch for the $location
      scope.$watch(function() {
        return $location.path();
      }, function(newValue, oldValue) {
        $('li[data-match-route]', element).each(function(k, li) {
          var $li = angular.element(li),
            // data('match-rout') does not work with dynamic attributes
            pattern = $li.attr('data-match-route'),
            regexp = new RegExp('^' + pattern + '$', ['i']);
          if(regexp.test(newValue)) {
            $li.addClass('active');
          } else {
            $li.removeClass('active');
          }
        });
      });
    }
  };
});

使用这个指令:

  1. 从http://mgcrea.github.io/angular-strap/下载AngularStrap

  2. 在bootstrap.js之后的页面中包含脚本:
    <script src="lib/angular-strap.js"></script>

  3. 将这些指令添加到你的模块中:
    angular.module('myApp', ['$strap.directives'])

  4. 将指令添加到导航栏:
    <div class="navbar" bs-navbar>

  5. 为每个导航项添加正则表达式:
    <li data-match-route="/about"><a href="#/about">About</a></li>

这里有一个简单的方法可以很好地与Angular一起工作。

<ul class="nav navbar-nav">
    <li ng-class="{ active: isActive('/View1') }"><a href="#/View1">View 1</a></li>
    <li ng-class="{ active: isActive('/View2') }"><a href="#/View2">View 2</a></li>
    <li ng-class="{ active: isActive('/View3') }"><a href="#/View3">View 3</a></li>
</ul>

在AngularJS控制器中:

$scope.isActive = function (viewLocation) {
     var active = (viewLocation === $location.path());
     return active;
};

如果你使用的是Angular的路由器,那么RouterLinkActive . _指令可以非常优雅地使用:

<ul class="navbar-nav">
  <li class="nav-item"><a class="nav-link" routerLink="home" routerLinkActive="active">Home</a></li>
  <li class="nav-item"><a class="nav-link" routerLink="gallery" routerLinkActive="active">Gallery</a></li>
  <li class="nav-item"><a class="nav-link" routerLink="pricing" routerLinkActive="active">Prices</a></li>
  <li class="nav-item"><a class="nav-link" routerLink="contact" routerLinkActive="active">Contact</a></li>
</ul>

首先,这个问题可以通过多种方式解决。这种方式可能不是最优雅的,但它确实有效。

这是一个简单的解决方案,你应该能够添加到任何项目。当你配置路由时,你可以添加一个"pageKey"或其他属性,你可以用它来关闭。此外,你可以在$route对象的$ routechangessuccess方法上实现一个监听器,以监听路由更改的成功完成。

当处理程序触发时,您获得页键,并使用该键来定位该页需要"ACTIVE"的元素,并应用ACTIVE类。

请记住,你需要一种方法使所有的元素" in ACTIVE"。正如你所看到的,我在我的导航项上使用。PAGEKEY类来关闭它们,我使用。pagekey_ {PAGEKEY}来单独打开它们。将它们全部切换为非活动状态会被认为是一种幼稚的方法,使用前面的方法只使活动项不活动可能会获得更好的性能,或者您可以更改jquery选择器,只选择要使活动项不活动。使用jquery来选择所有活动项可能是最好的解决方案,因为它可以确保当前路由的所有内容都被清理干净,以防之前路由中可能存在的任何css错误。

这意味着改变这行代码:

$(".pagekey").toggleClass("active", false);

到这个

$(".active").toggleClass("active", false);
下面是一些示例代码:

给定一个引导导航栏

<div class="navbar navbar-inverse">
    <div class="navbar-inner">
        <a class="brand" href="#">Title</a>
        <ul class="nav">
            <li><a href="#!/" class="pagekey pagekey_HOME">Home</a></li>
            <li><a href="#!/page1/create" class="pagekey pagekey_CREATE">Page 1 Create</a></li>
            <li><a href="#!/page1/edit/1" class="pagekey pagekey_EDIT">Page 1 Edit</a></li>
            <li><a href="#!/page1/published/1" class="pagekey pagekey_PUBLISH">Page 1 Published</a></li>
        </ul>
    </div>
</div>

和一个angular模块和控制器,如下所示:

<script type="text/javascript">
    function Ctrl($scope, $http, $routeParams, $location, $route) {
    }

    angular.module('BookingFormBuilder', []).
        config(function ($routeProvider, $locationProvider) {
            $routeProvider.
                when('/', { 
                   template: 'I''m on the home page', 
                   controller: Ctrl, 
                   pageKey: 'HOME' }).
                when('/page1/create', { 
                   template: 'I''m on page 1 create', 
                   controller: Ctrl, 
                   pageKey: 'CREATE' }).
                when('/page1/edit/:id', { 
                   template: 'I''m on page 1 edit {id}', 
                   controller: Ctrl, pageKey: 'EDIT' }).
                when('/page1/published/:id', { 
                   template: 'I''m on page 1 publish {id}', 
                   controller: Ctrl, pageKey: 'PUBLISH' }).
                otherwise({ redirectTo: '/' });
            $locationProvider.hashPrefix("!");
        }).run(function ($rootScope, $http, $route) {
            $rootScope.$on("$routeChangeSuccess", 
                           function (angularEvent, 
                                     currentRoute,
                                     previousRoute) {
                var pageKey = currentRoute.pageKey;
                $(".pagekey").toggleClass("active", false);
                $(".pagekey_" + pageKey).toggleClass("active", true);
            });
        });
</script>

你可以使用angular-ui-utils' ui-route指令:

<a ui-route ng-href="/">Home</a>
<a ui-route ng-href="/about">About</a>
<a ui-route ng-href="/contact">Contact</a>

或:

头控制器
/**
 * Header controller
 */
angular.module('myApp')
  .controller('HeaderCtrl', function ($scope) {
    $scope.menuItems = [
      {
        name: 'Home',
        url:  '/',
        title: 'Go to homepage.'
      },
      {
        name:   'About',
        url:    '/about',
        title:  'Learn about the project.'
      },
      {
        name:   'Contact',
        url:    '/contact',
        title:  'Contact us.'
      }
    ];
  });

索引页

<!-- index.html: -->
<div class="header" ng-controller="HeaderCtrl">
  <ul class="nav navbar-nav navbar-right">
    <li ui-route="{{menuItem.url}}" ng-class="{active: $uiRoute}"
      ng-repeat="menuItem in menuItems">
      <a ng-href="#{{menuItem.url}}" title="{{menuItem.title}}">
        {{menuItem.name}}
      </a>
    </li>
  </ul>
</div>

如果你正在使用ui-utils,你可能也会对ui-router管理部分/嵌套视图感兴趣。

我觉得所有这些答案对我来说都有点太复杂了,抱歉。所以我创建了一个小指令,应该在每个导航栏的基础上工作:

app.directive('activeLink', function () {
    return {
        link: function (scope, element, attrs) {
            element.find('.nav a').on('click', function () {
                angular.element(this)
                    .parent().siblings('.active')
                    .removeClass('active');
                angular.element(this)
                    .parent()
                    .addClass('active');
            });
        }
    };
});

用法:

<ul class="nav navbar-nav navbar-right" active-link>
    <li class="nav active"><a href="home">Home</a></li>
    <li class="nav"><a href="foo">Foo</a></li>
    <li class="nav"><a href="bar">Bar</a></li>
</ul>

我使用ng-class指令和$location来实现它。

<ul class="nav">
<li data-ng-class="{active: ($location.path() == '/') }">
    <a href="#/">Carpeta Amarilla</a>
</li>
<li class="dropdown" data-ng-class="{active: ($location.path() == '/auditoria' || $location.path() == '/auditoria/todos') }">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#">
        Auditoria
        <b class="caret"></b>
    </a>
    <ul class="dropdown-menu pull-right">
        <li data-ng-class="{active: ($location.path() == '/auditoria') }">
            <a href="#/auditoria">Por Legajo</a>
        </li>
        <li data-ng-class="{active: ($location.path() == '/auditoria/todos') }">
            <a href="#/auditoria/todos">General</a>
        </li>
    </ul>
</li>
</ul>

它要求导航栏位于主控制器内部,并可以像这样访问$location服务:

bajasApp.controller('MenuCntl', ['$scope','$route', '$routeParams', '$location', 
   function MenuCntl($scope, $route, $routeParams, $location) {
   $scope.$route = $route;
   $scope.$location = $location;
   $scope.$routeParams = $routeParams;
}]);

如果您使用ui-router,下面的示例应该满足您的需求,基于@DanPantry对接受的答案的评论,而无需添加任何控制器端代码:

<div class="collapse navbar-collapse" ng-controller="HeaderController">
    <ul class="nav navbar-nav">
        <li ui-sref-active="active"><a ui-sref="app.home()" href="/">Home</a></li>
        <li ui-sref-active="active"><a ui-sref="app.dogs()" href="/dogs">Dogs</a></li>
        <li ui-sref-active="active"><a ui-sref="app.cats()" href="/cats">Cats</a></li>
    </ul>
</div>
<div ng-view></div>

您可以查看文档获取更多信息

你可以在angular表达式中使用条件来实现这一点,例如:

<a href="#" class="{{ condition ? 'active' : '' }}">link</a>

话虽这么说,我确实发现angular指令是更"合适"的方式,尽管外包很多这种迷你逻辑可能会在某种程度上污染你的代码库。

我在开发过程中每隔一段时间就会使用条件来进行GUI样式设计,因为它比创建指令要快一点。我不能告诉你一个实例,它们实际上在代码库中保留了很长时间。最后,我要么把它变成一个指令,要么找到一个更好的方法来解决问题。

如果你不想使用AngularStrap,那么这个指令就可以了!这是对https://stackoverflow.com/a/16231859/910764的修改。

JavaScript

angular.module('myApp').directive('bsNavbar', ['$location', function ($location) {
  return {
    restrict: 'A',
    link: function postLink(scope, element) {
      scope.$watch(function () {
        return $location.path();
      }, function (path) {
        angular.forEach(element.children(), (function (li) {
          var $li = angular.element(li),
            regex = new RegExp('^' + $li.attr('data-match-route') + '$', 'i'),
            isActive = regex.test(path);
          $li.toggleClass('active', isActive);
        }));
      });
    }
  };
}]);

<ul class="nav navbar-nav" bs-navbar>
  <li data-match-route="/home"><a href="#/home">Home</a></li>
  <li data-match-route="/about"><a href="#/about">About</a></li>
</ul>

注意:上面的HTML类假设你使用的是Bootstrap 3。x

这是我的看法。在这篇文章中找到了一些答案。我有一个稍微不同的情况,所以我的解决方案包括将菜单分离到自己的模板中,以便在指令定义对象中使用,然后将导航条添加到我需要它的页面上。基本上,我有一个登录页面,我不想包含我的菜单,所以我使用了ngInclude并在登录时插入这个指令:

<标题>指令:
module.directive('compModal', function(){

return {
    restrict: 'E',
    replace: true,
    transclude: true,
    scope: true,
    templateUrl: 'templates/menu.html',
    controller: function($scope, $element, $location){
        $scope.isActive = function(viewLocation){
            var active = false;
            if(viewLocation === $location.path()){
                active = true;
            }
            return active;
        }
    }
 }
});

指令模板(templates/menu.html)

<ul class="nav navbar-nav">
  <li ng-class="{ active: isActive('/View1') }"><a href="#/View1">View 1</a></li>
  <li ng-class="{ active: isActive('/View2') }"><a href="#/View2">View 2</a></li>
  <li ng-class="{ active: isActive('/View3') }"><a href="#/View3">View 3</a></li>
</ul>
包含指令的HTML
<comp-navbar/>

希望这有助于

扩展我的答案,我需要这个来处理这样的结构。

指数

-events<积极
——事件列表
——event-edit
——事件映射& lt;点击

——
——place-list
——place-edit
——place-map

因此,我必须使用indexOf而不是匹配,以便验证格式化为匹配条件的子链接。所以对于'events':

<li ng-class="{ active: isActive('/event')}" class="divider-vertical dropdown">

function NavController($scope, $location) { 
$scope.isActive = function (viewLocation) {
    var s=false;
    if($location.path().indexOf(viewLocation) != -1){
     s = true;
    }
    return s;
};}

这是一个简单的解决方案

<ul class="nav navbar-nav navbar-right navbar-default menu">
  <li ng-class="menuIndice == 1 ? 'active':''">
    <a ng-click="menuIndice = 1" href="#/item1">item1</a>
  </li>
  <li ng-class="menuIndice == 2 ? 'active':''">
    <a ng-click="menuIndice = 2" href="#/item2">item2</a>
  </li>
  <li ng-class="menuIndice == 3 ? 'active':''">
    <a ng-click="menuIndice = 3" href="#/item3">item3</a>
  </li>
</ul>

使用一个对象作为switch变量
您可以很简单地使用:

<ul class="nav navbar-nav">
   <li ng-class="{'active':switch.linkOne}" ng-click="switch = {linkOne: true}"><a href="/">Link One</a></li>
   <li ng-class="{'active':switch.linkTwo}" ng-click="switch = {link-two: true}"><a href="/link-two">Link Two</a></li>
</ul>

每次你点击一个链接,开关对象被替换为一个新的对象,只有正确的开关对象属性为真。未定义的属性将被计算为false,因此依赖于它们的元素将不会被分配活动类。

结合@Olivier的AngularStrap答案,我也实现了kevinknelson的答案:https://github.com/twbs/bootstrap/issues/9013。

本来,Bootstrap3导航条并不是为单页(比如Angular)应用程序设计的,因此在小屏幕上的菜单不会在点击时折叠。

JavaScript

/**
 * Main AngularJS Web Application
 */
var app = angular.module('yourWebApp', [
    'ngRoute'
]);

/**
 * Setup Main Menu
 */
app.controller('MainNavCtrl', [ '$scope', '$location', function ( $scope, $location) {
    $scope.menuItems = [
        {
            name: 'Home',
            url:  '/home',
            title: 'Welcome to our Website'
        },
        {
            name: 'ABOUT',
            url:  '/about',
            title: 'Know about our work culture'
        },
        {
            name:   'CONTACT',
            url:    '/contact',
            title:  'Get in touch with us'
        }
    ];
    $scope.isActive = function (viewLocation) {
        return viewLocation === $location.path();
    };
}]);

  <div class="navbar-collapse collapse" ng-controller="MainNavCtrl">
    <ul id="add-magic-line" class="nav navbar-nav navbar-right">
      <li data-ng-class="{current_page_item: isActive('{{ menuItem.url }}')}" data-ng-repeat="menuItem in menuItems">
        <a data-ng-href="#{{menuItem.url}}" title="{{menuItem.title}}">
          {{menuItem.name}}
        </a>
      </li>
    </ul>
  </div>

感谢@Pylinux。我使用了他的技术,并对其进行了修改,以支持"一"级下拉菜单(sub/ul/li),因为这是我需要的。在下面的小提琴链接中看到它的作用。

基于pylinux的答案更新了Fiddle - http://jsfiddle.net/abhatia/en4qxw6g/

为了支持一个级别的下拉菜单,我做了以下三个更改:
1。添加了一个类值dd(下拉)的"a"元素下的li需要有子ul列表。

         <li><a class="dd">This link points to #/fun5</a>
          <ul>
            <li><a href="#/fun6?some=data">This link points to #/fun6</a>
            </li>
            <li><a href="#/fun7?some=data">This link points to #/fun7</a>
            </li>
            <li><a href="#/fun8?some=data">This link points to #/fun8</a>
            </li>
            <li><a href="#/fun9?some=data">This link points to #/fun9</a>
            </li>
          </ul>
        </li>


2。更新Javascript,添加以下新逻辑。

 if(angular.element(li).parent().parent().children('a').hasClass("dd"))
 {angular.element(li).parent().parent().children('a.dd').addClass('active');}


3。更新CSS,增加以下内容:

a.active {background-color:red;}

希望这将有助于有人希望实现单级下拉菜单。

你也可以使用这个active-link指令https://stackoverflow.com/a/23138152/1387163

父类li将在location匹配/url:

时获得活动类
<li>
    <a href="#!/url" active-link active-link-parent>
</li>

我建议在链接上使用指令。这是小提琴

但它还不完美。注意hashbang;)

下面是javascript for指令:

angular.module('link', []).
  directive('activeLink', ['$location', function(location) {
    return {
      restrict: 'A',
      link: function(scope, element, attrs, controller) {
        var clazz = attrs.activeLink;
        var path = attrs.href;
        path = path.substring(1); //hack because path does not return including hashbang
        scope.location = location;
        scope.$watch('location.path()', function(newPath) {
          if (path === newPath) {
            element.addClass(clazz);
          } else {
            element.removeClass(clazz);
          }
        });
      }
    };
  }]);

,下面是它在html中的用法:

<div ng-app="link">
  <a href="#/one" active-link="active">One</a>
  <a href="#/two" active-link="active">One</a>
  <a href="#" active-link="active">home</a>
</div>
css:

.active{ color:red; }

只是为了在辩论中添加我的两分钱,我做了一个纯angular模块(没有jquery),它也将与包含数据的哈希url一起工作。(e.g. #/this/is/path?this=is&some=data)

您只需将模块作为依赖项和auto-active添加到菜单的祖先之一。这样的:

<ul auto-active>
    <li><a href="#/">main</a></li>
    <li><a href="#/first">first</a></li>
    <li><a href="#/second">second</a></li>
    <li><a href="#/third">third</a></li>
</ul>

模块是这样的:

(function () {
    angular.module('autoActive', [])
        .directive('autoActive', ['$location', function ($location) {
        return {
            restrict: 'A',
            scope: false,
            link: function (scope, element) {
                function setActive() {
                    var path = $location.path();
                    if (path) {
                        angular.forEach(element.find('li'), function (li) {
                            var anchor = li.querySelector('a');
                            if (anchor.href.match('#' + path + '(?=''?|$)')) {
                                angular.element(li).addClass('active');
                            } else {
                                angular.element(li).removeClass('active');
                            }
                        });
                    }
                }
                setActive();
                scope.$on('$locationChangeSuccess', setActive);
            }
        }
    }]);
}());

*(当然你可以只使用指令部分)

**值得注意的是,这并不适用于空哈希(例如example.com/#example.com),它需要至少有example.com/#/example.com#/。但这在ngResource之类的代码中会自动发生。

  • 这里是小提琴:http://jsfiddle.net/gy2an/8/
  • 这里是github: https://github.com/Karl-Gustav/autoActive
  • 这是我的原始答案:https://stackoverflow.com/a/22282124/1465640

这个对我很有用:

  var domain = '{{ DOMAIN }}'; // www.example.com or dev.example.com
  var domain_index =  window.location.href.indexOf(domain);
  var long_app_name = window.location.href.slice(domain_index+domain.length+1); 
  // this turns http://www.example.com/whatever/whatever to whatever/whatever
  app_name = long_app_name.slice(0, long_app_name.indexOf('/')); 
  //now you are left off with just the first whatever which is usually your app name

然后使用jquery(也适用于angular)添加类active

$('nav a[href*="' + app_name+'"]').closest('li').addClass('active');

当然还有css:

.active{background:red;}

如果你的HTML像这样:

<ul><li><a href="/ee">ee</a></li><li><a href="/dd">dd</a></li></ul>

这将自动添加类活动使用页面url和颜色您的背景为红色,如果你在www.somesite.com/ee比ee是'应用程序',它将是活跃的

这是很长的回答,但我想我会分享我的方式:

.run(function($rootScope, $state){
 $rootScope.$state = $state;
});

模板:

<ul class="nav navbar-nav">
    <li ng-class="{ active: $state.contains('View1') }"><a href="...">View 1</a></li>
    <li ng-class="{ active: $state.contains('View2') }"><a href="...">View 2</a></li>
    <li ng-class="{ active: $state.contains('View3') }"><a href="...">View 3</a></li>
</ul>

对于使用ui-router的用户:

<ul class="nav navbar-nav">
        <li ui-sref-active="active"><a href="...">View 1</a></li>
        <li ui-sref-active="active"><a href="...">View 2</a></li>
        <li ui-sref-active="active"><a href="...">View 3</a></li>
</ul>

对于精确匹配(例如嵌套状态?)使用$state.name === 'full/path/to/state'ui-sref-active-eq="active"

这里是另一个解决方案,任何人可能感兴趣。这样做的好处是它具有更少的依赖性。见鬼,它没有web服务器也可以工作。所以它完全是客户端。

HTML:

<nav class="navbar navbar-inverse" ng-controller="topNavBarCtrl"">
<div class="container-fluid">
    <div class="navbar-header">
        <a class="navbar-brand" href="#"><span class="glyphicon glyphicon-home" aria-hidden="true"></span></a>
    </div>
    <ul class="nav navbar-nav">
        <li ng-click="selectTab()" ng-class="getTabClass()"><a href="#">Home</a></li>
        <li ng-repeat="tab in tabs" ng-click="selectTab(tab)" ng-class="getTabClass(tab)"><a href="#">{{ tab }}</a></li>
    </ul>
</div>

解释:

这里我们使用指令ng-repeat从angularjs模型动态生成链接。下面这个导航条的控制器中定义了selectTab()getTabClass()方法,神奇的事情发生了。

控制器:

angular.module("app.NavigationControllersModule", [])
// Constant named 'activeTab' holding the value 'active'. We will use this to set the class name of the <li> element that is selected.
.constant("activeTab", "active")
.controller("topNavBarCtrl", function($scope, activeTab){
    // Model used for the ng-repeat directive in the template.
    $scope.tabs = ["Page 1", "Page 2", "Page 3"];
    var selectedTab = null;
    // Sets the selectedTab.
    $scope.selectTab = function(newTab){
       selectedTab = newTab;
    };
    // Sets class of the selectedTab to 'active'.
    $scope.getTabClass = function(tab){
       return selectedTab == tab ? activeTab : "";
    };
});

解释:

selectTab()方法使用ng-click指令调用。因此,当单击链接时,将变量selectedTab设置为该链接的名称。在HTML中,您可以看到这个方法在没有任何参数的情况下调用Home选项卡,以便在页面加载时突出显示。

getTabClass()方法是通过HTML中的ng-class指令调用的。该方法检查它所在的选项卡是否与selectedTab变量的值相同。如果为true,则返回"active",否则返回",这是ng-class指令应用的类名。然后,无论您应用于active类的css将应用于所选选项卡。

只需添加所需的active -class和所需的颜色代码。

示例:ng-class="{'active': currentNavSelected}" ng-click="setNav"