Question
You have an array rendered inside a ul, with one li for each element, and a controller property called selectedIndex. How can you add a CSS class only to the li whose index matches selectedIndex in AngularJS?
For example, instead of manually duplicating the li markup, adding a class to one copy, and using ng-show / ng-hide to display only one version per index, what is the recommended AngularJS approach?
A simplified version might look like this:
<ul>
<li ng-repeat="item in items">
{{ item.name }}
</li>
</ul>
And in the controller:
$scope.selectedIndex = 2;
Short Answer
By the end of this page, you will understand how AngularJS conditionally applies CSS classes using ngClass, especially when rendering lists with ngRepeat. You will also see how $index works, how to compare it with selectedIndex, and why this is better than duplicating HTML with ng-show and ng-hide.
Concept
In AngularJS, the standard way to conditionally apply a CSS class is with the ngClass directive.
When you render a list using ngRepeat, AngularJS gives you access to a special local variable called $index, which represents the current item's position in the loop. If your controller stores a selectedIndex, you can compare $index to that value and apply a class when they match.
This matters because:
- It keeps your template small and readable.
- It avoids duplicated markup.
- It separates structure from styling.
- It makes updates easier when selection changes.
Instead of rendering two versions of the same li, you render one li and let AngularJS decide whether it should include a class.
This is a common UI pattern in real applications:
- selected menu items
- active tabs
- highlighted search results
- chosen rows in a table
- current step in a wizard
In short, ngClass is the AngularJS tool for dynamic styling based on application state.
Mental Model
Think of each li as a student in a line, and selectedIndex as the number of the student who should wear a badge.
You do not create two copies of every student—one with a badge and one without. Instead, you keep one student and simply ask:
- “Is this the selected one?”
- If yes, give them the badge.
- If no, do nothing.
In AngularJS, ngRepeat gives each item a number ($index), and ngClass decides whether that item gets the CSS "badge" class.
Syntax and Examples
The most common syntax is:
<li ng-repeat="item in items"
ng-class="{ selected: $index === selectedIndex }">
{{ item.name }}
</li>
Example
<ul>
<li ng-repeat="item in items"
ng-class="{ selected: $index === selectedIndex }">
{{ item.name }}
</li>
</ul>
$scope.items = [
{ name: 'Apple' },
{ name: 'Banana' },
{ name: 'Cherry' }
];
$scope.selectedIndex = 1;
.selected {
background-color: #dbeafe;
font-weight: bold;
}
What this does
Step by Step Execution
Consider this example:
<ul>
<li ng-repeat="item in items"
ng-class="{ selected: $index === selectedIndex }">
{{ item.name }}
</li>
</ul>
$scope.items = [
{ name: 'A' },
{ name: 'B' },
{ name: 'C' }
];
$scope.selectedIndex = 2;
Step-by-step
- AngularJS reads
itemsand startsngRepeat. - For the first item:
item.nameisA$indexis0- condition is
0 === 2, which isfalse - no class is added
Real World Use Cases
Conditional classes are used everywhere in AngularJS interfaces.
Navigation menus
Highlight the current menu item:
<li ng-repeat="link in links"
ng-class="{ active: $index === selectedIndex }">
{{ link.label }}
</li>
Tabs
Mark the open tab:
<button ng-repeat="tab in tabs"
ng-class="{ activeTab: $index === selectedIndex }">
{{ tab.title }}
</button>
Search results
Emphasize the currently focused result for keyboard navigation.
Data tables
Highlight the selected row in an admin dashboard.
Image galleries
Apply a border to the currently selected thumbnail.
Forms and validation
Add classes like error, valid, or dirty based on state.
In all of these cases, the idea is the same: the class reflects data or UI state.
Real Codebase Usage
In real AngularJS projects, developers commonly use ngClass with simple conditions and keep the selection state in the controller.
Common pattern: selected index
<li ng-repeat="item in items"
ng-class="{ selected: $index === selectedIndex }"
ng-click="selectedIndex = $index">
{{ item.name }}
</li>
This is a standard pattern for clickable lists.
Guarding against invalid values
Developers often make sure selectedIndex is valid before using it.
if ($scope.selectedIndex < 0 || $scope.selectedIndex >= $scope.items.length) {
$scope.selectedIndex = 0;
}
Using item identity instead of index
In larger codebases, developers sometimes track the selected item by ID instead of index, because indexes can change if the list is filtered, sorted, or reordered.
<li ng-repeat=
=>
{{ item.name }}
Common Mistakes
Here are common beginner mistakes when conditionally applying classes in AngularJS.
1. Duplicating markup with ng-show and ng-hide
Not recommended
<li ng-repeat="item in items" ng-show="$index === selectedIndex" class="selected">
{{ item.name }}
</li>
<li ng-repeat="item in items" ng-hide="$index === selectedIndex">
{{ item.name }}
</li>
This creates unnecessary duplication and makes templates harder to maintain.
Better
<li ng-repeat="item in items"
ng-class="{ selected: $index === selectedIndex }">
{{ item.name }}
</li>
2. Using class for dynamic logic
Broken approach:
Comparisons
Here is how ngClass compares with related AngularJS approaches.
| Approach | Best for | Good choice here? | Notes |
|---|---|---|---|
ng-class | Conditionally adding/removing CSS classes | Yes | Clean, readable, standard solution |
class | Static classes only | No | Does not handle AngularJS conditional logic by itself |
ng-show / ng-hide | Showing or hiding entire elements | No | Changes visibility, not styling |
ng-style | Dynamic inline styles | Sometimes | Useful for one-off styles, but classes are usually cleaner |
Cheat Sheet
<li ng-repeat="item in items"
ng-class="{ selected: $index === selectedIndex }">
{{ item.name }}
</li>
Key points
- Use
ngClassfor conditional CSS classes. - Use
$indexinsidengRepeat. - Compare
$indexwithselectedIndex. - Prefer object syntax for readability.
Common forms
Object syntax
ng-class="{ selected: $index === selectedIndex }"
Ternary syntax
ng-class="$index === selectedIndex ? 'selected' : ''"
Multiple classes
ng-class="{ selected: isSelected, disabled: isDisabled }"
Rules
$indexexists only insidengRepeat.
FAQ
How do I add a class to one item in an AngularJS ngRepeat?
Use ng-class and compare $index with your selected value.
<li ng-repeat="item in items" ng-class="{ selected: $index === selectedIndex }">
Is ngClass better than ng-show and ng-hide for this?
Yes. ngClass is the correct tool when you want to style an element differently. ng-show and ng-hide are for visibility, not class changes.
What is $index in AngularJS?
$index is a local variable available inside ngRepeat. It contains the current loop position starting from 0.
Can I use a class name string instead of object syntax in ngClass?
Mini Project
Description
Build a selectable list of items in AngularJS. The user should be able to click any item in the list, and the clicked item should receive a highlight class. This project demonstrates how ngRepeat, $index, ngClick, and ngClass work together in a practical UI pattern.
Goal
Create an AngularJS list where exactly one item is highlighted based on selectedIndex, and clicking an item changes the selection.
Requirements
- Render a list of items using
ngRepeat. - Store the selected item position in
selectedIndex. - Apply a CSS class only when
$index === selectedIndex. - Update
selectedIndexwhen a user clicks an item. - Add visible CSS so the selected item is clearly highlighted.
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
CSS :not() Selector for Elements Without a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common pitfalls.
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.