MOCKSTACKS
EN
Questions And Answers

More Tutorials









Angular ngRepeat


ng-repeat is a built in directive in Angular which lets you iterate an array or an object and gives you the ability to repeat an element once for each item in the collection.

ng-repeat an array

<ul>
 <li ng-repeat="item in itemCollection">
 {{item.Name}}
 </li>
</ul>


Where:
item = individual item in the collection
itemCollection = The array you are iterating

ng-repeat an object

<ul>
 <li ng-repeat="(key, value) in myObject">
 {{key}} : {{value}}
 </li>
</ul>


Where:
key = the property name
value = the value of the property
myObject = the object you are iterating

filter your ng-repeat by user input

<input type="text" ng-model="searchText">
<ul>
 <li ng-repeat="string in stringArray | filter:searchText">
 {{string}}
 </li>
</ul>


Where:
searchText = the text that the user wants to filter the list by
stringArray = an array of strings, e.g. ['string', 'array']
You can also display or reference the filtered items elsewhere by assigning the filter output an alias with as aliasName, like so:

<input type="text" ng-model="searchText">
<ul>
 <li ng-repeat="string in stringArray | filter:searchText as filteredStrings">
 {{string}}
 </li>
</ul>
<p>There are {{filteredStrings.length}} matching results</p>


ng-repeat-start and ng-repeat-end

To repeat multiple DOM elements by defining a start and an end point you can use the ng-repeat-start and ngrepeat-end directives.

<ul>
 <li ng-repeat-start="item in [{a: 1, b: 2}, {a: 3, b:4}]">
 {{item.a}}
 </li>
 <li ng-repeat-end>
 {{item.b}}
 </li>
</ul>
Output
1
2
3
4


It is important to always close ng-repeat-start with ng-repeat-end.

Performance considerations

Rendering ngRepeat can become slow, especially when using large collections .If the objects in the collection have an identifier property, you should always track by the identifier instead of the whole object, which is the default functionality. If no identifier is present, you can always use the built-in $index.

<div ng-repeat="item in itemCollection track by item.id">
<div ng-repeat="item in itemCollection track by $index">


Scope of ngRepeat

ngRepeat will always create an isolated child scope so care must be taken if the parent scope needs to be accessed inside the repeat.
Here is a simple example showing how you can set a value in your parent scope from a click event inside of ngRepeat.

scope val: {{val}}<br/>
ctrlAs val: {{ctrl.val}}
<ul>
 <li ng-repeat="item in itemCollection">
 <a href="#" ng-click="$parent.val=item.value; ctrl.val=item.value;">
 {{item.label}} {{item.value}}
 </a>
 </li>
</ul>
$scope.val = 0;
this.val = 0;
$scope.itemCollection = [{
 id: 0,
 value: 4.99,
 label: 'Football'
},
{
 id: 1,
 value: 6.99,
 label: 'Baseball'
},
{
 id: 2,
 value: 9.99,
 label: 'Basketball'
}];


If there was only val = item.value at ng-click it won't update the val in the parent scope because of the isolated scope. That's why the parent scope is accessed with $parent reference or with the controllerAs syntax (e.g. ngcontroller="mainController as ctrl").

Nested ng-repeat
You can also use nested ng-repeat.

<div ng-repeat="values in test">
 <div ng-repeat="i in values">
 [{{$parent.$index}},{{$index}}] {{i}}
 </div>
</div>

var app = angular.module("myApp", []);
app.controller("ctrl", function($scope) {
 $scope.test = [
 ['a', 'b', 'c'],
 ['d', 'e', 'f']
 ];
});



Conclusion

In this page (written and validated by ) you learned about AngularJS ngRepeat . What's Next? If you are interested in completing AngularJS tutorial, your next topic will be learning about: AngularJS Built In Directives Cheat Sheet.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.