How to set active tab style with AngularJS ?
Last Updated :
08 Sep, 2022
In this article, we will see how to set an active tab style using AngularJS, & will also understand its implementation through the example. This task can be accomplished by implementing the isActive and the ng-controller method. We will perform this task with 2 different methods.
Method 1: The ng-controller Directive in AngularJS is used to add a controller to the application. It can be used to add methods, functions, and variables that can be called on some event like click, etc to perform certain actions.
Syntax:
<element ng-controller="expression">
Contents...
</element>
The below example implements the above approach.
Example 1: This example describes setting the active tab style using AngularJS.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to set active tab style with AngularJS?
</title>
</head>
<body>
<div class="collapse navbar-collapse"
ng-controller="HeaderController">
<ul class="nav navbar-nav">
<li ng-class="{ active: isActive('/')}">
<a href="/">Geeks For Geeks</a>
</li>
<li ng-class="{ active: isActive('/html')}">
<a href="/html">HTML</a>
</li>
<li ng-class="{ active: isActive('/js')}">
<a href="/js">JAVASCRIPT</a>
</li>
</ul>
</div>
<div ng-view></div>
<script>
function HeaderController($scope, $location) {
$scope.isActive = function(viewLocation) {
return viewLocation === $location.path();
};
}
</script>
</body>
</html>
Output:

Method 2: Here we will use the modular function in Angular JS to create a module. A module is created by using the AngularJS function angular.module.
Syntax: for creating a module:
<div ng-app="myFirstApp">...</div>
<script>
var app = angular.module("myFirstApp", []);
//myFirstApp refers to HTML element in which application runs.
</script>
Syntax: for adding a directive to the module:
<div ng-app="myApp"></div>
<script>
var my_app = angular.module("myFirstApp", []);
my_app.directive("DirectiveApp", function() {
return {
template : "Hello Geeks!!!"
};
});
</script>
Example 2: This is another example that describes setting the active tab style using AngularJS.
HTML
<!DOCTYPE html>
<html>
<head>
<title>How to set active tab style with AngularJS?</title>
</head>
<body>
<div ng-app="link">
<a href="#Geeks For Geeks" active-link="active">
Geeks For Geeks
</a>
<a href="#HTML" active-link="active">HTML</a>
<a href="#JAVASCRIPT" active-link="active">JAVASCRIPT</a>
</div>
<script>
angular.module('link', []).directive('Link',
['$location', function(location) {
return {
link: function(scope, element, attrs) {
var active = attrs.activeLink;
var path = attrs.href;
path = path.substring(1);
scope.location = location;
scope.$watch('location.path()', function(newPath) {
if(path === newPath) {
element.addClass(active);
} else {
element.removeClass(active);
}
});
}
};
}]);
</script>
</body>
</html>
Output:

Similar Reads
How to Set navbar active class with Bootstrap and AngularJS ? In this article, we will see how to set the navbar active class with Bootstrap and AngularJS, & will know its implementation through the example. To accomplish this task, you can use ng-controller(NavigationController) to set the bootstrap navbar active class with AngularJS. To run a single cont
2 min read
How to determine active route in AngularJS ? In this article, we will see how to determine the active route in AngularJS, along with understanding the basic implementation through the example. Approach: To determine which is the active route at any moment in AngularJS this can be achieved by using $on() & $location.path() method. An event
2 min read
How to Style an Active Link in VueJS ? In single-page applications effective navigation becomes crucial. In this article, we'll explore how to style active links in Vue.js using the popular routing library, Vue-router. Table of Content Using router-link-exact-active classConditional class bindingSteps to Setup the Project EnvironmentStep
4 min read
How to check which tab is active using Material UI ? Material-UI is one of the most popular React UI libraries. Material-UI components work in isolation. They are self-supporting, and will only inject the styles they need to display. They don't rely on any global style-sheets such as normalize.css. Some examples of Material UI components are Dialog, T
3 min read
How to Open URL in New Tab using Angular ? In this article, we will learn How to open a link in a new tab using Angular. A Link is a connection from one web page to another web page. Generally, the anchor tag can be used to open URLs in a new tab in an elementary and straightforward manner. However, the window.open() method can also be utili
2 min read
Styling Active Router Link in Angular Styling the active router link in angular allows the user to differentiate between the active router link and inactive router links. Angular provides a special mechanism to work with active router links. Approach: Create the Angular app to be used.Create the header component that contains the navi
2 min read