AngularJS ng-repeat
Yönergesi
Örnek
Kayıtlar dizisindeki her öğe için bir başlık yazın:
<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>
Tanım ve Kullanım
Yönerge ng-repeat
, belirli sayıda HTML kümesini tekrarlar.
HTML kümesi, bir koleksiyondaki öğe başına bir kez tekrarlanacaktır.
Koleksiyon bir dizi veya nesne olmalıdır.
Not: Tekrarın her örneğine, geçerli öğeden oluşan kendi kapsamı verilir.
Bir nesne koleksiyonunuz varsa, ng-repeat
yönerge bir HTML tablosu yapmak, her nesne için bir tablo satırı ve her nesne özelliği için bir tablo verisi görüntülemek için mükemmeldir. Aşağıdaki örneğe bakın.
Sözdizimi
<element ng-repeat="expression"></element>
Tüm HTML öğeleri tarafından desteklenir.
Parametre Değerleri
Value | Description |
---|---|
expression | An expression that specifies how to loop the collection. Legal Expression examples: x in records
(key, value) in myObj |
Daha fazla örnek
Örnek
Kayıtlar dizisindeki her öğe için bir tablo satırı yazın:
<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>
Örnek
Bir nesnedeki her özellik için bir tablo satırı yazın:
<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>