AngularJS ng-repeat
Directive
Example
Write one header for each item in the records array:
<body ng-app="myApp" ng-controller="myCtrl">
<h1 ng-repeat="x in
records">{{x}}</h1>
<script>
var app = angular.module("myApp",
[]);
app.controller("myCtrl", function($scope) {
$scope.records
= [
"Alfreds Futterkiste",
"Berglunds snabbköp",
"Centro
comercial Moctezuma",
"Ernst
Handel",
]
});
</script>
</body>
Try it Yourself »
Definition and Usage
The ng-repeat
directive repeats a set of HTML, a given number of
times.
The set of HTML will be repeated once per item in a collection.
The collection must be an array or an object.
Note: Each instance of the repetition is given its own scope, which consist of the current item.
If you have an collection of objects, the ng-repeat
directive is
perfect for making a HTML table, displaying one table row for each object,
and one table data for each object property. See example below.
Syntax
<element ng-repeat="expression"></element>
Supported by all HTML elements.
Parameter Values
Value | Description |
---|---|
expression | An expression that specifies how to loop the collection. Legal Expression examples: x in records
(key, value) in myObj |
More Examples
Example
Write one table row for each item in the records array:
<table ng-controller="myCtrl" border="1">
<tr ng-repeat="x
in records">
<td>{{x.Name}}</td>
<td>{{x.Country}}</td>
</tr>
</table>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl",
function($scope) {
$scope.records = [
{
"Name" : "Alfreds Futterkiste",
"Country" : "Germany"
},{
"Name" : "Berglunds snabbköp",
"Country" : "Sweden"
},{
"Name" : "Centro comercial Moctezuma",
"Country" : "Mexico"
},{
"Name" : "Ernst Handel",
"Country" : "Austria"
}
]
});
</script>
Try it Yourself »
Example
Write one table row for each property in an object:
<table ng-controller="myCtrl" border="1">
<tr ng-repeat="(x,
y) in myObj">
<td>{{x}}</td>
<td>{{y}}</td>
</tr>
</table>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl",
function($scope) {
$scope.myObj = {
"Name" : "Alfreds Futterkiste",
"Country" : "Germany",
"City"
: "Berlin"
}
});
</script>
Try it Yourself »