Angular JS Toggle Active Class on Path Change using Ui-Router

Getting Started

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

index.html

This is the main page of our app.
  1. <!DOCTYPE html>
  2. <html ng-app="app">
  3. <title>Angular JS Toggle Active on Path Change using Ui-Router</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="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.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('/home')"><a ui-sref="home">Home</a></li>
  24. <li ng-class="active('/about')"><a ui-sref="about">About</a></li>
  25. <li ng-class="active('/blog')"><a ui-sref="blog">Blog</a></li>
  26. </ul>
  27. </div>
  28. </div>
  29. </nav>
  30. <div class="container">
  31. <div ui-view></div>
  32. </div>
  33. <script src="app.js"></script>
  34. </body>
  35. </html>
We have use Bootstrap to create our navbar and we put ng-class which toggles our active function and we pass the path of the menu as a parameter.

app.js

This contains our angular js scripts. .config is where we set the routes of our app and in .controller, we have our active function which returns active whenever the passed parameter is the same as the current path.
  1. var app = angular.module('app', ['ui.router']);
  2.  
  3. app.config(function($stateProvider, $urlRouterProvider) {
  4.  
  5. $urlRouterProvider.otherwise('/home');
  6.  
  7. $stateProvider
  8.  
  9. .state('home', {
  10. url: '/home',
  11. templateUrl: 'home.html'
  12. })
  13.  
  14. .state('about', {
  15. url: '/about',
  16. templateUrl: 'about.html',
  17.  
  18. })
  19.  
  20. .state('blog', {
  21. url: '/blog',
  22. templateUrl: 'blog.html',
  23. });
  24.  
  25. });
  26.  
  27. app.controller('mainCtrl', function($scope, $location){
  28. $scope.active = function(path){
  29. return ($location.path() === path) ? 'active' : '';
  30. }
  31. });

Creating our Paths

The ff are the templates for the paths in this tutorial. 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