The Wayback Machine - https://web.archive.org/web/20240712053627/https://www.geeksforgeeks.org/angular-js-ng-repeat-directive/
Open In App

Angular-JS ng-repeat Directive

Last Updated : 09 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Angular-JS ng-repeat directive is a handy tool to repeat a set of HTML code for a number of times or once per item in a collection of items. ng-repeat is mostly used on arrays and objects.
ng-repeat is similar to a loop that we have in C, C++ or other languages but technically it instantiates a template(normally a set of HTML structures) for each element in a collection that we are accessing. Angular maintains a $index variable as a key to the element which is currently being accessed and a user can also access this variable.

Syntax :

<div ng-repeat="keyName in MyObjectName ">
  {{keyName}}
</div>

Here “MyObjectName” is a collection that can be an object or an array and you can access each value inside it using a “keyName”.

Example 1

  1. Create an app.js file for the app.




    var app = angular.module('myApp',[]);
      
    app.controller('MainCtrl', function($scope){
        $scope.names = ['Adam','Steve','George','James','Armin'];
        console.log($scope.names);
    });

    
    

    Line 1- Created an app module named “myApp” with no dependencies.
    Line 3- Main controller for our application.
    Line 4- Array of strings “names”.

  2. Create index.html page




    <!DOCTYPE html>
    <html ng-app="myApp">
    <head>
         <title>Angular ng-repeat</title>
         <script> type="text/javascript" src="jquery-3.2.1.min.js">
                                                          </script>
         <script> type="text/javascript" src="angular.js"></script>
         <script> type="text/javascript" src="app.js"></script>
    </head>
    <body ng-controller="MainCtrl">
         <h2>Here is the name list</h2>
         <ul>
            <li ng-repeat="name in names">
                {{name}}
            </li>
         </ul>
    </body>
    </html> 

    
    

    Line 5- Include all the dependencies like jquery, angular-js and app.js file
    Line 12- Use ng-repeat directive to get one name from names array at a time and display it.

  3. Output :
    Example 2

  1. app.js file




    var app = angular.module('myApp',[]);
      
    app.controller('MainCtrl', function($scope){
        $scope.strings= ['Geeks','For','Geeks'];
        console.log($scope.strings);
    });

    
    

  2. We have a list of three strings.

    index.html




    <!DOCTYPE html>
    <html ng-app="myApp">
    <head>
         <title>Angular ng-repeat</title>
         <script> type="text/javascript" src="jquery-3.2.1.min.js">
                                                          </script>
         <script> type="text/javascript" src="angular.js"></script>
         <script> type="text/javascript" src="app.js"></script>
    </head>
    <body ng-controller="MainCtrl">
         <h2>Here is the string list</h2>
         <ul>
            <li ng-repeat="s in strings>
                {{name}}
            </li>
         </ul>
    </body>
    </html> 

    
    

    Note-“track by $index” is used here because there are duplicate entries in our list i.e. “Geeks”. Duplicate keys are not allowed because AngularJS uses keys to associate DOM nodes with items. “track by $index”, will cause the items to be keyed by their position in the array instead of their value

  3. Output :

  4. Applications:

    1. ng-repeat can be used to iterate through an array which requires less lines of code than the usual javascript method.
    2. Filters can be used with ng-repeat to create an easy to implement search bar.

    References



Similar Reads

How to access index of the parent ng-repeat from child ng-repeat in AngularJS ?
In this article, we will see How to access the index of the parent ng-repeat from child ng-repeat in AngularJS. In AngularJS applications, when we implement the nested ng-repeat directives and if we need to access the index of the parent ng-repeat from the child ng-repeat, then we can do this by using 2 different approaches like by using the $paren
6 min read
How to Add Dynamic Options for Multiple Selects Inside ng-repeat Directive ?
Given an HTML document containing some options element and the task is to add an array of javascript objects dynamically with multiple selects using ng-repeat in angularJS. Approach: The task is done using ng-repeat which loops through an array. Let's call this array "models". Each select menu present in the DOM is modeled to its specific index in
3 min read
How to limit ngFor repeat to some number of items in Angular ?
In AngularJS, we can use the ngFor directive to loop over a list of items and display them on a webpage. Although, there could be some cases where we don't want to use all the elements in the list but want to display only a limited number of initial items, or the paginated items from the list. In this article, we will see how to limit the ngFor dir
3 min read
Angular forms NgForm Directive
In this article, we are going to see what is NgForm in Angular 10 and how to use it. NgForm is used to create a top-level form group Instance, and it binds the form to the given form value. Syntax: &lt;form #FormName = "ngForm"&gt; &lt;/form&gt; NgModule: Module used by NgForm is: FormsModule Selectors: [ngForm] Approach: Create the Angular app to
1 min read
Angular forms NgModel Directive
In this article, we are going to see what is NgModel in Angular 10 and how to use it. NgModel is used to create a top-level form group Instance, and it binds the form to the given form value. Syntax: &lt;Input [(NgModel)= 'name']&gt; NgModule: Module used by NgModel is: FormsModule Selectors: [ngModel] Approach: Create the Angular app to be usedIn
1 min read
Angular 10 NgIf Directive
In this article, we are going to see what is NgIf in Angular 10 and how to use it. The ngIf Directive in Angular10 is used to remove or recreate a portion of HTML element based on an expression. If the expression inside it is false then the element is removed and if it is true then the element is added to the DOM. Syntax: &lt;li *ngIf='condition'
2 min read
Angular 10 NgTemplateOutlet Directive
In this article, we are going to see what is NgTemplateOutlet in Angular 10 and how to use it. The NgTemplateOutlet in Angular10 is used to insert an embedded view from a prepared TemplateRef. NgTemplateOutlet adds the reference element which is displayed on the page. Syntax: &lt;li *NgTemplateOutlet='condition'&gt;&lt;/li&gt; NgModule: Module used
1 min read
Angular 10 NgPlural Directive
In this article, we are going to see what is NgPlural in Angular 10 and how to use it. The NgPlural in Angular10 is used to Add or remove DOM sub-trees based on a numeric value. Syntax: &lt;li *NgPlural='condition'&gt;&lt;/li&gt; NgModule: Module used by NgPlural is: CommonModule Selectors: [NgPlural] Approach: Create an Angular app that to be used
1 min read
Angular forms FormArrayName Directive
In this article, we are going to see what is Style in Angular 10 and how to use it. The FormArrayName is used to sync a nested FormArray to a DOM element. Syntax: &lt;div formArrayName="arrayName"&gt; NgModule: Module used by FormArrayName is: ReactiveFormsModule Selectors: [FormArrayName] Approach: Create the Angular app to be usedIn app.component
1 min read
Angular forms FormControlName Directive
In this article, we are going to see what is FormControlName in Angular 10 and how to use it. FormControlName is used to sync a FormControl in an existing FormGroup to a form control element by name. Syntax: &lt;form [FormControlName] ="name"&gt; Exported from: ReactiveFormsModule Selectors: [FormControlName] Approach: Create the Angular app to be
1 min read
Article Tags :
three90RightbarBannerImg