ຄຳສັ່ງ AngularJS ng-repeat

ການອະທິບາຍແລະການນໍາໃຊ້

ng-repeat ຄຳສັ່ງຈະກວດຄືນສະໝອງ HTML ອອກຈາກຄຳສັ່ງຈຳນວນທີ່ກໍານົດ.

ແຕ່ລະບັນດາໃນສະມາຊິກ HTML ຈະຖືກກວດຄືນຫຼາຍຄັ້ງ.

ສະມາຊິກຄວນເປັນສະໝອງຫຼືບັນດາບັນຊີ.

ເຫັນຫຍັງ:ການລະບຸແຕ່ລະບັນດາຈະມີບັນຊີບັນນາທິບາຍຂອງເອງ, ບັນນາທິບາຍປະກອບມີບັນດາບັນຊີໃນປະຈຸບັນ.

ຖ້າທ່ານມີສະມາຊິກບັນດາບັນຊີ:ng-repeat ຄຳສັ່ງໄດ້ຫຼາຍຢູ່ທີ່ສ້າງ HTML ຕາບັນຊີສຳລັບແຕ່ລະບັນດາບັນຊີຫຼືຜະນຶກໃນບັນດາບັນຊີຫຼືຜະນຶກອອກຂໍ້ມູນຂອງບັນດາບັນຊີ. ການພິສູດຢູ່ລາຍການລາຍລະອຽດນີ້.

ບົດສະຫຼຸບ

ຕົວຢ່າງ 1

ຂຽນການປະກອບຫົວຂໍ້ສຳລັບແຕ່ລະບັນດາໃນລະຫັດ 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>

Try it yourself

ຕົວຢ່າງ 2

ຂຽນການປະກອບວຽກບັນຊີສຳລັບແຕ່ລະບັນດາໃນລະຫັດ 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>

Try it yourself

ຕົວຢ່າງ 3

ຂຽນການປະກອບວຽກບັນຊີສຳລັບແຕ່ລະຜະນຶກໃນບັນດາບັນຊີ:

<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

Syntax

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

All HTML elements are supported.

Parameters

Parameters Description
expression

Expression specifying how to iterate over a collection.

Legal expression example:

x in records

(key, value) in myObj

x in records track by $id(x)