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 Create an app.js file for the app. javascript 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". Create index.html page 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 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. Output : Example 2 app.js file javascript var app = angular.module('myApp',[]); app.controller('MainCtrl', function($scope){ $scope.strings= ['Geeks','For','Geeks']; console.log($scope.strings); }); We have a list of three strings. index.html 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 Output : Applications: ng-repeat can be used to iterate through an array which requires less lines of code than the usual javascript method. 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 Comment More info N neerajnegi174 Follow Improve Article Tags : AngularJS Explore AngularJS BasicsAngularJS Tutorial 5 min read Introduction to AngularJS 4 min read Angular CLI | Angular Project Setup 3 min read AngularJS Expressions 2 min read AngularJS Modules 3 min read AngularJS ng-model Directive 4 min read AngularJS Data Binding 4 min read AngularJS Controllers 3 min read AngularJS | Scope 2 min read AngularJS Services 4 min read AngularJS | AJAX - $http 3 min read AngularJS | Tables 2 min read AngularJS Select Boxes 2 min read AngularJS SQL 3 min read AngularJS HTML DOM 2 min read AngularJS Events 3 min read AngularJS | Forms 3 min read AngularJS Form Validation 3 min read AngularJS | API 2 min read AngularJS and W3.CSS 2 min read AngularJS Includes 3 min read AngularJS Animations 1 min read AngularJS | Application 3 min read AngularJS DirectivesAngularJS Directives 9 min read AngularJS ng-app Directive 1 min read AngularJS ng-bind Directive 2 min read AngularJS ng-bind-html Directive 2 min read AngularJS ng-bind-template Directive 2 min read AngularJS ng-blur Directive 1 min read AngularJS ng-change Directive 2 min read AngularJS ng-checked Directive 2 min read AngularJS ng-class Directive 2 min read AngularJS ng-class-even Directive 2 min read AngularJS ng-class-odd Directive 2 min read AngularJS ng-click Directive 2 min read AngularJS ng-cloak Directive 2 min read AngularJS ng-controller Directive 2 min read AngularJS Directives Complete Reference 2 min read AngularJS FiltersAngularJS | Filters 7 min read AngularJS currency Filter 2 min read AngularJS | date Filter 2 min read AngularJS filter Filter 3 min read AngularJS json Filter 2 min read AngularJS limitTo Filter 2 min read AngularJS lowercase Filter 1 min read AngularJS number Filter 1 min read AngularJS orderBy Filter 4 min read AngularJs uppercase Filter 1 min read AngularJS Converting FunctionsAngularJS angular.lowercase() Function 2 min read AngularJS angular.uppercase() Function 1 min read AngularJS angular.forEach() Function 1 min read AngularJS Comparing FunctionsAngularJS angular.isArray() Function 2 min read AngularJS angular.isDate() Function 2 min read AngularJS angular.isDefined() Function 2 min read AngularJS angular.isElement() Function 2 min read AngularJS angular.isFunction() Function 2 min read AngularJS angular.isNumber() Function 2 min read AngularJS angular.isObject() Function 2 min read AngularJS | angular.isString() Function 1 min read AngularJS angular.isUndefined() Function 2 min read AngularJS angular.equals() Function 2 min read AngularJS angular.toJson() Function 2 min read AngularJS QuestionsHow to bundle an Angular app for production? 4 min read How to add many functions in one ng-click directive? 2 min read How to directly update a field by using ng-click in AngularJS ? 3 min read How to Add Dynamic Options for Multiple Selects Inside ng-repeat Directive ? 3 min read How to detect when an @Input() value changes in Angular? 3 min read How to open popup using Angular and Bootstrap ? 2 min read How to reload or re-render the entire page using AngularJS? 2 min read How to add input fields dynamically on button click in AngularJS ? 2 min read How to Create Button Dynamically with Click Event in Angular ? 2 min read How to use jQuery in Angular ? 2 min read AngularJS Examples 2 min read Like