Improve this Doc View Source ngRepeat
- directive in module ng
The ngRepeat
directive instantiates a template once per item from a collection. Each template instance gets its own scope, where the given loop variable is set to the current collection item, and $index
is set to the item index or key.
Special properties are exposed on the local scope of each template instance, including:
Variable | Type | Details |
---|---|---|
$index | number | iterator offset of the repeated element (0..length-1) |
$first | boolean | true if the repeated element is first in the iterator. |
$middle | boolean | true if the repeated element is between the first and last in the iterator. |
$last | boolean | true if the repeated element is last in the iterator. |
$even | boolean | true if the iterator position $index is even (otherwise false). |
$odd | boolean | true if the iterator position $index is odd (otherwise false). |
Creating aliases for these properties is possible with ngInit
. This may be useful when, for instance, nesting ngRepeats.
Special repeat start and end points
To repeat a series of elements instead of just one parent element, ngRepeat (as well as other ng directives) supports extending the range of the repeater by defining explicit start and end points by using ng-repeat-start and ng-repeat-end respectively. The ng-repeat-start directive works the same as ng-repeat, but will repeat all the HTML code (including the tag it's defined on) up to and including the ending HTML tag where ng-repeat-end is placed.
The example below makes use of this feature:
<header ng-repeat-start="item in items"> Header {{ item }} </header> <div class="body"> Body {{ item }} </div> <footer ng-repeat-end> Footer {{ item }} </footer>
And with an input of ['A','B']
for the items variable in the example above, the output will evaluate to:
<header> Header A </header> <div class="body"> Body A </div> <footer> Footer A </footer> <header> Header B </header> <div class="body"> Body B </div> <footer> Footer B </footer>
The custom start and end points for ngRepeat also support all other HTML directive syntax flavors provided in AngularJS (such as data-ng-repeat-start, x-ng-repeat-start and ng:repeat-start).
Directive Info
- This directive creates new scope.
- This directive executes at priority level 1000.
Usage
- as attribute:
<ANY ng-repeat=""> ... </ANY>
Animations
.enter - when a new item is added to the list or when an item is revealed after a filter
.leave - when an item is removed from the list or when an item is filtered out
.move - when an adjacent item is filtered out causing a reorder or when the item contents are reordered
Click here to learn more about the steps involved in the animation.Arguments
Param | Type | Details |
---|---|---|
ngRepeat | repeat_expression | The expression indicating how to enumerate a collection. These formats are currently supported:
|
This example initializes the scope to a list of names and then uses ngRepeat
to display every person:
© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.2.32/docs/api/ng/directive/ngRepeat