ng-repeatArahan AngularJS


Contoh

Tulis satu header untuk setiap item dalam larik record:

<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>

Definisi dan Penggunaan

Arahan ng-repeatmengulangi satu set HTML, beberapa kali.

Kumpulan HTML akan diulang sekali per item dalam koleksi.

Koleksi harus berupa array atau objek.

Catatan: Setiap contoh pengulangan diberikan ruang lingkupnya sendiri, yang terdiri dari item saat ini.

Jika Anda memiliki kumpulan objek, ng-repeatdirektif sangat cocok untuk membuat tabel HTML, menampilkan satu baris tabel untuk setiap objek, dan satu data tabel untuk setiap properti objek. Lihat contoh di bawah.


Sintaksis

<element ng-repeat="expression"></element>

Didukung oleh semua elemen HTML.


Nilai Parameter

Value Description
expression An expression that specifies how to loop the collection.

Legal Expression examples:

x in records

(key, value) in myObj

x in records track by $id(x)


Lebih Banyak Contoh

Contoh

Tulis satu baris tabel untuk setiap item dalam larik record:

<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>

Contoh

Tulis satu baris tabel untuk setiap properti dalam suatu objek:

<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>