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 infoAdvertise with us Next Article Angular-JS ng-repeat Directive N neerajnegi174 Follow 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-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 1 min read Like