add Bootstrap active class based on route change in Angular


I am trying to add a class (bootstrap active class) using Angular ngClass on my navigation menu, based on the path.
navigationMain template:

<div class="section">
<div class="container">
    <div class="row">
        <div class="col-md-12" ng-controller="MainCtrl">
            <ul class="nav nav-pills nav-justified" style="width:30%">
                <li ng-class="{active: location === '#!/home', nav-item}">
                    <a href="#!/home">Home</a>
                <li ng-class="{active: location === '#!/resume', nav-item}">
                    <a href="#!/resume">CV</a>
                <li ng-class="{active: location === '#!/overviewActivities', nav-item}">
                    <a href="#!/overviewActivities">I-Talent</a>


    module('iTalentApp', [
]).controller('MainCtrl', function ($scope, $rootscope, $location) {
    $scope.location = $location.path();
    $rootScope.$on('$routeChangeSuccess', function () {
        $scope.location = $location.path();


    <div ng-view></div>

And my config:

config(['$locationProvider', '$routeProvider',
    function config($locationProvider, $routeProvider) {

            when('/home', {
                template: '<home></home>'
            when('/resume', {
                template: '<resume></resume>'
            when('/overviewActivities', {
                template: '<overview-activities></overview-activities>'

I tried to implement the solution to this question, adding or removing classes based on route changes in angular but to no avail. Can anyone point out what I’m doing wrong?

this is what i’m trying to achieve but in a single page with angular, I got it working except the conditional active class when they are used. (I’m also fairly new to html, css and js)


angular.js:14199 Error: [$injector:unpr] Unknown provider: $rootscopeProvider <- $rootscope <- MainCtrl$injector/unpr?p0=%24rootscopeProvider%20%3C-%20%24rootscope%20%3C-%20MainCtrl
    at angular.js:68
    at angular.js:4563
    at Object.getService [as get] (angular.js:4716)
    at angular.js:4568
    at getService (angular.js:4716)
    at injectionArgs (angular.js:4741)
    at Object.invoke (angular.js:4763)
    at $controllerInit (angular.js:10592)
    at nodeLinkFn (angular.js:9469)
    at compositeLinkFn (angular.js:8810)


Since we found what was wrong in the comments I’ll take the time to write an answer for future reference, The problems were:
– spelling error in controller, changed $rootscope into $rootScope,
– wrong use of ng-class syntax, removed nav-item.
– mistake in the expression of ng-class, location===’#!/path’ changed into location===’/path’.
Kudos to @dfsq

Answered By – Erik

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.