Angular JS Toggle Active Class on Path Change using ng-Route

Getting Started

I've used CDN for Bootstrap, Angular JS and Angular Route so you need internet connection for them to work.

index.html

This is the main view of our app.
  1. <!DOCTYPE html>
  2. <html ng-app="app">
  3. <title>Angular JS Toggle Active Class on Path Change using ng-Route</title>
  4. <meta charset="utf-8">
  5. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  7. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-route.min.js"></script>
  8. </head>
  9. <body ng-controller="mainCtrl">
  10. <nav class="navbar navbar-default">
  11. <div class="container">
  12. <div class="navbar-header">
  13. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
  14. <span class="sr-only">Toggle navigation</span>
  15. <span class="icon-bar"></span>
  16. <span class="icon-bar"></span>
  17. <span class="icon-bar"></span>
  18. </button>
  19. <a class="navbar-brand" href="www.sourcecodester.com">SourceCodeSter</a>
  20. </div>
  21. <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
  22. <ul class="nav navbar-nav">
  23. <li ng-class="active('/')"><a href="#/">Home</a></li>
  24. <li ng-class="active('/about')"><a href="#/about">About</a></li>
  25. <li ng-class="active('/blog')"><a href="#/blog">Blog</a></li>
  26. </ul>
  27. </div>
  28. </div>
  29. </nav>
  30. <div class="container">
  31. <div ng-view></div>
  32. </div>
  33. <script src="app.js"></script>
  34. </body>
  35. </html>
In here, we used Bootstrap navbar to create our menu and we use ng-class to toggle active function that will send our defined parameter. The active function will then compared the sent parameter to the current path and will return active if matched.

app.js

This contains our angular js script. .config is where we setup the each of our path and .controller is where active function is found.
  1. var app = angular.module('app', ['ngRoute']);
  2.  
  3. app.config(function($routeProvider){
  4. $routeProvider
  5. .when('/', {
  6. templateUrl: 'home.html'
  7. })
  8. .when('/about', {
  9. templateUrl: 'about.html'
  10. })
  11. .when('/blog', {
  12. templateUrl: 'blog.html'
  13. })
  14. .otherwise({
  15. redirectTo: '/'
  16. });
  17. });
  18.  
  19. app.controller('mainCtrl', function($scope, $location){
  20. $scope.active = function(path){
  21. return ($location.path() === path) ? 'active' : '';
  22. }
  23. });

Creating our Path Templates

The ff. are the template for our route. home.html
  1. <h4>This is the Home Page</h4>
  2. <p>See that the <b>Home Tab</b> is active</p>
about.html
  1. <h4>This is the AboutPage</h4>
  2. <p>See that the <b>AboutTab</b> is active</p>
blog.html
  1. <h4>This is the Blog Page</h4>
  2. <p>See that the <b>Blog Tab</b> is active</p>
That ends this tutorial. Happy Coding :)

Add new comment