Angular Add and remove Active class on click ng toggle

Angular Add and remove Active class on click ng toggle

Today, We want to share with you Angular Add and remove Active class on click ng toggle.
In this post we will show you How to Add and remove active class on click using AngularJS, hear for Add Class, Remove Class or Toggle Class to div using AngularJS we will give you demo and example for implement.
In this post, we will learn about Add or Remove css class to HTML element in angularJS with an example.

angular active class on click

see I have step by step explained (Add active class to an active element append class) about adding a new active class on click button and change.

READ :  Angularjs json stringify - angularjs parse json - angular to Json

But if we need to change nav menu to remove the active class on button clicking the item again more times.
now Let us check and easy ro understand how it will work.

The web app used to app header and app footer of an application basic need are typically outside the angularjs scope of all the content or angularjs controller you are working on.
This makes active class menu and managing the navigation different state a little more to challenging.

In my apps case, to managing active class to navigation state is add class to simply applying an active class to nav menu a particular element.

READ :  Angular 6 CRUD Operations Application Tutorials




  
  <title>angularjs menu active class</title>


<div class="account-item">
<div class="account-heading">
<h4 class="account-title">
        <a href="#/NavMessagesDisplay"> {{ navitem }}</a>
      </h4>
   </div>
</div>



angular active class on click File Name : app.js

var navapp = angular.module('navmenuapp', []);
navapp.controller('navmenuMainCtrl', function($scope) {
 $scope.listmenuItemsdata = ['Home','Tools','About Us','Services','Portfolio','Contact', 'Products', 'Other','Download'];
$scope.navmenuactiveMenu = $scope.listmenuItemsdata[0];
   $scope.navsetActivemenu = function(navmenuItemname) {
    $scope.navmenuactiveMenu = navmenuItemname
 }
});

style.css

/* include or Put your css in here style.css file */

.active {
  background : red;
}

Alternate Way Simple – angular active class on click

A very Simple and elegant – esay way is to use angularjs ng-controller to run a single controller used to outside of the ng-view data changes:

<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
		//List of Navigation menu in li html tag
<li><a href="/">Home</a></li>
<li><a href="/AboutUs">AboutUs</a></li>
<li><a href="/Services">Services</a></li>
<li><a href="/PORTFOLIO">PORTFOLIO</a></li>
<li><a href="/Products">Products</a></li>
<li><a href="/Career">Career</a></li>
<li><a href="/Contact">Contact</a></li>
<li><a href="/Tools">Tools</a></li>
<li><a href="/Privacy">Privacy</a></li>
<li><a href="/Terms">Terms</a></li>
    </ul>
</div>
<div></div>

and specific path to include in controllers.js:

HeaderMenucontrollers.js

function NavHeaderMenuController($scope, $location) 
{ 
    $scope.navisActiveMenu = function (setviewLocationname) { 
        return setviewLocationname === $location.path();
    };
}

Add a Comment

Your email address will not be published. Required fields are marked *