Instruksi AngularJS ng-repeat
Definisi dan Penggunaan
ng-repeat
Instruksi akan mengulang sekumpulan HTML sebanyak yang disebutkan.
Setiap item dalam koleksi HTML akan diulang sekali.
Koleksi harus berupa array atau objek.
Perhatian:Setiap instansiasi yang diulang akan mendapatkan lingkungan konteks sendiri, yang terdiri dari proyek saat ini.
Jika Anda memiliki koleksi objek:ng-repeat
Instruksi sangat cocok untuk membuat tabel HTML, menampilkan baris tabel untuk setiap objek, dan menampilkan data properti untuk setiap objek. Lihat contoh di bawah ini.
Contoh
Contoh 1
Tulis judul untuk setiap item di daftar records:
<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>
Contoh 2
Tulis baris tabel untuk setiap item di daftar records:
<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 3
Tulis baris tabel untuk setiap properti 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>
Syarat
<element ng-repeat="ekspresi</element>
Semua elemen HTML mendukung.
Parameter
Parameter | Deskripsi |
---|---|
ekspresi |
Menentukan bagaimana ekspresi mengelilingi kumpulan. Contoh ekspresi yang sah:
|