Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • AngularJS Tutorial
  • AngularJS Directives
  • AngularJS Functions
  • AngularJS Filters
  • AngularJS Examples
  • AngularJS Interview Questions
  • Angular ngx Bootstrap
  • AngularJS Cheat Sheet
  • AngularJS PrimeNG
  • JavaScript
  • Web Technology
Open In App
Next Article:
AngularJS ng-open Directive
Next article icon

Angular-JS ng-repeat Directive

Last Updated : 09 Aug, 2019
Comments
Improve
Suggest changes
Like Article
Like
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

  • https://angularjs.org/
  • https://docs.angularjs.org/api/ng/directive/ngRepeat
  • https://docs.angularjs.org/error/ngRepeat/dupes


Next Article
AngularJS ng-open Directive

N

neerajnegi174
Improve
Article Tags :
  • AngularJS

Similar Reads

  • AngularJS ng-paste Directive
    The ng-paste Directive in AngularJS is used to specify custom behavior functions when the text in input fields is pasted into an HTML element. It can be used to call a function that will be triggered when the text is pasted into the input field. It is supported by <input>, <select> and
    2 min read
  • AngularJS ng-srcset Directive
    The ng-srcset Directive in AngularJS is used to specify the srcset attribute of an <img> element, ie, overriding the original srcset attribute of an <img> element. It helps to ensure that the wrong image is not produced until AngularJS has been evaluated. This directive must be used inst
    2 min read
  • AngularJS ng-open Directive
    The ng-open Directive in AngularJS is used to specify the open attribute of the specified HTML element. If the expression inside the ng-open directive returns true then the details will be shown otherwise they will be hidden. Syntax: <element ng-open="expression"> Contents... <element> P
    1 min read
  • AngularJS ng-pattern Directive
    The ng-pattern Directive in AngularJS is used to add up pattern (regex pattern) validator to ng-Model on an input HTML element. It is used to set the pattern validation error key if input field data does not match a RegExp that is found by evaluating the Angular expression specified in the ng-patter
    2 min read
  • AngularJS ng-src Directive
    The ng-src Directive in AngularJS is used to specify the src attribute of an <img> element, ie., it overrides the original src attribute for an <img> element. This attribute must be used if we have the Angular code inside of the src element. It ensures that the wrong image is not produce
    2 min read
  • AngularJS | ng-selected Directive
    The ng-selected Directive in AngularJS is used to specify the selected attribute of an HTML element. It can be used to select the default value specified on an HTML element. If the expression inside the ng-selected directive returns true then the selected option value will be displayed otherwise not
    2 min read
  • AngularJS ng-readonly Directive
    The ng-readonly Directive in AngularJS is used to specify the readonly attribute of an HTML element. The HTML element will be readonly only if the expression inside the ng-readonly directive returns true. The ng-readonly directive is required to change the value between true and false. In case, if t
    2 min read
  • AngularJS ng-required Directive
    The ng-required Directive in AngularJS is used to specify the required attribute of an HTML element. The input field in the form is required only if the expression inside the ng-required directive returns true. It is supported by <input>, <select> and <textarea> tags. Syntax: <e
    2 min read
  • AngularJS ng-options Directive
    The ng-options Directive in AngularJS is used to build and bind HTML elements with options to a model property. It is used to specify <options> in a <select> list. It is designed specifically to populate the items on a dropdown list. This directive implements an array, in order to fill t
    2 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: <form #FormName = "ngForm"> </form> NgModule: Module used by NgForm is: FormsModule Select
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences