---
title: "Explain the Loops in AngularJS"  
description: "Loops are effectively handled using the ng-repeat directive, which allows for the dynamic generation of HTML elements based on collections or objects."  
author: "Ashutosh Patel"  
published: 2024-06-21  
updated: 2024-06-21  
canonical: https://www.mindstick.com/blog/304406/explain-the-loops-in-angularjs  
category: "angular js"  
tags: ["javascript", "angular js", "angularjs ng repeat"]  
reading_time: 3 minutes  

---

# Explain the Loops in AngularJS

#### Angularjs Loops

Loops are often handled with instructions such as [ng-repeat](https://www.mindstick.com/interview/33921/explain-the-concept-of-ng-repeat-in-angularjs). This guide allows you to iterate object **collections** in your AngularJS [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) and [dynamically create HTML](https://www.mindstick.com/forum/158688/how-can-you-dynamically-create-html-elements-using-jquery) objects based on each element in the collection.

Let’s explore the use of loops in AngularJS,

**Using the ng-repeat Directive**\
The ng-repeat instruction is used to iterate over a collection (such as an array or object) and create HTML elements for each object in the collection.

```html
<div ng-app="myApp" ng-controller="MyController">
  <ul>
    <li ng-repeat="item in items">{{ item }}</li>
  </ul>
</div>
```

## In this example

- `ng-app="myApp"` Starts an AngularJS application.
- `ng-controller="MyController"` associates the controller with a `MyController` div, which scopes the objects.
- `ng-repeat="object in objects"` Repeats the object array in scope and provides a <li> element for each object in the array.
- `{{ item }}` Displays the current item in the list.

**Controller** (`MyController`)

```javascript
angular.module('myApp', [])
  .controller('MyController', function($scope) {
    $scope.items = ['Apple', 'Banana', 'Cherry'];
  });
```

**Iterating over Objects**\
You can also use `ng-repeat` to iterate over the attributes of the object,

```html
<div ng-app="myApp" ng-controller="MyController">
  <ul>
    <li ng-repeat="(key, value) in myObject">{{ key }}: {{ value }}</li>
  </ul>
</div>
```

In this case, the **key** represents the **property name**, and the value represents the corresponding **value** in **myObject**.

\
**Filters using** `ng-repeat`\
AngularJS provides filters to enable `ng-repeat`. For example, you can use `orderBy` to `filter` items or select items based on specific criteria.

```html
<ul>
  <li ng-repeat="item in items | orderBy:'name'">{{ item.name }}</li>
</ul>
```

**Track by Expression**\
When iterating over objects that may be modified or reordered, it is good practice to use `track by` to [improve performance](https://www.mindstick.com/forum/160287/how-can-parameterized-stored-procedures-improve-performance-compared-to-dynamic-sql-queries) and maintain [relationships](https://www.mindstick.com/blog/11850/how-to-build-stronger-relationships-with-your-employees) between DOM elements and their associated data objects.

```html
<ul>
  <li ng-repeat="item in items track by item.id">{{ item.name }}</li>
</ul>
```

#### Performance Considerations

- Avoid using `ng-repeat` on large collections or heavy packages, as it can affect performance. Use infinite pages or scrolling to efficiently handle [large amounts of data](https://www.mindstick.com/forum/161107/how-does-mongodb-handle-large-amounts-of-data-and-maintain-performance).
- Use a `track by` with a unique identifier (`item.id`) to improve performance by helping AngularJS identify unique items.

## Example-

```html
<!DOCTYPE html>
<html data-ng-app="myApp" lang="en">
<head>
  <meta charset="UTF-8">
  <title>AngularJS Loop Example</title>
  <!-- bootstrap cdn link -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <!-- angular js cdn link -->
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body>

<div data-ng-controller="MyController">
    <div class="text-start">
        <h2>AngularJS Loops Example</h2>
        <p class="fs-5 fw-semibold m-1">Fruits List</p>
        <ul>
            <!-- bind the data using ng-repeat loop -->
          <li data-ng-repeat="fruit in fruits">
            {{ fruit.name }} - {{ fruit.color }}
          </li>
        </ul>
    </div>
</div>

<script>
  angular.module('myApp', [])
    .controller('MyController', function($scope) {
        // data collection
      $scope.fruits = [
        { name: 'Apple', color: 'Red' },
        { name: 'Banana', color: 'Yellow' },
        { name: 'Cherry', color: 'Red' },
        { name: 'Grapes', color: 'Green' }
      ];
    });
</script>

</body>
</html>
```

**Output-**\

![Explain the Loops in AngularJS](https://www.mindstick.com/blogs/b292942e-92b5-455f-9030-abeaf6e2c824/images/8e61c909-cfae-4593-8024-4fb8ee48fe4a.jpg)

Loops are best handled with the `ng-repeat` directive, which allows dynamic HTML objects to be created based on collections or objects in application scope Like using `ng-repeat` with filters and `track by`expressions so [Ensure efficient](https://www.mindstick.com/forum/159987/how-does-react-s-reconciliation-algorithm-work-and-how-does-it-ensure-efficient-updates-virtual-dom) and flexible [data binding](https://www.mindstick.com/articles/337466/exploring-data-binding-in-mvc-how-it-connects-model-and-view) in your AngularJS [applications](https://www.mindstick.com/articles/12847/how-to-choose-the-right-ethernet-cable-for-industrial-applications) does so .

**Also, Read:** [Explain Routing in AngularJS](https://www.mindstick.com/blog/304405/explain-routing-in-angularjs)

---

Original Source: https://www.mindstick.com/blog/304406/explain-the-loops-in-angularjs

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
