AngularJS ng-repeatDirektive
AngularJS-Beispiel
Mehrere Überschriften durchlaufen und ausgeben:
<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 = [
"Beispiel-Tutorial 1",
"Beispiel-Tutorial 2",
"Beispiel-Tutorial 3",
"Beispiel-Tutorial 4",
]
});
</script>
</body>
<h1 ng-repeat="x in records">{{x}}</h1>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = [
"Beispiel-Tutorial 1",
"Beispiel-Tutorial 2",
"Beispiel-Tutorial 3",
"Beispiel-Tutorial 4",
]
});
</script>
</body>
Selbst ausprobieren »
Definition und Verwendung
ng-repeatDie Direktive wird verwendet, um HTML-Elemente eine bestimmte Anzahl von Malen zu durchlaufen und auszugeben.
Die Sammlung muss ein Array oder ein Objekt sein.
Syntax
<element ng-repeat="expression"></element>
Alle HTML-Elemente unterstützen diese Direktive.
Parameterwerte
| Wert | Beschreibung |
|---|---|
| expression | Der Ausdruck definiert, wie die Sammlung durchlaufen werden soll. Beispielregeln für Ausdrücke: x in records
(key, value) in myObj |
Weitere Beispiele
AngularJS-Beispiel
Ein Array verwenden, um eine Tabelle zu durchlaufen und auszugeben:
<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>
<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>
Selbst ausprobieren »
AngularJS-Beispiel
Ein Objekt verwenden, um eine Tabelle zu durchlaufen und auszugeben:
<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>
<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>
Selbst ausprobieren »
Andere Erweiterungen
AngularJS-Referenzhandbuch