ล่าสุดการพัฒนาเว็บบทเรียน
 

AngularJS NG-ซ้ำ Directive


ตัวอย่าง

เขียนหัวหนึ่งสำหรับแต่ละรายการในบันทึกอาร์เรย์:

<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 snabbkop",
        "Centro comercial Moctezuma",
        "Ernst Handel",
    ]
});
</script>

</body>
ลองตัวเอง»

ความหมายและการใช้งาน

ng-repeat สั่งซ้ำชุดของ HTML เป็นจำนวนครั้งที่กำหนด

ชุดของ HTML จะต้องทำซ้ำหนึ่งครั้งต่อหนึ่งรายการในคอลเลกชัน

คอลเลกชันต้องเป็นอาร์เรย์หรือวัตถุ

หมายเหตุ: ตัวอย่างของการทำซ้ำแต่ละคนจะได้รับขอบเขตของตัวเองซึ่งประกอบด้วยรายการปัจจุบัน

ถ้าคุณมีคอลเลกชันของวัตถุที่ ng-repeat คำสั่งเป็นที่สมบูรณ์แบบสำหรับการทำตาราง HTML ที่แสดงแถวของตารางหนึ่งสำหรับแต่ละวัตถุและเป็นหนึ่งในข้อมูลตารางสำหรับแต่ละวัตถุสถานที่ให้บริการ ดูตัวอย่างด้านล่าง


วากยสัมพันธ์

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

สนับสนุนโดยองค์ประกอบ HTML ทั้งหมด


ค่าพารามิเตอร์

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)

ตัวอย่างเพิ่มเติม

ตัวอย่าง

เขียนแถวของตารางหนึ่งสำหรับแต่ละรายการในบันทึกอาร์เรย์:

<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 snabbkop",
            "Country" : "Sweden"
        },{
            "Name" : "Centro comercial Moctezuma",
            "Country" : "Mexico"
        },{
            "Name" : "Ernst Handel",
            "Country" : "Austria"
        }
    ]
});
</script>
ลองตัวเอง»

ตัวอย่าง

เขียนแถวของตารางหนึ่งสำหรับคุณสมบัติในวัตถุแต่ละ

<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>
ลองตัวเอง»