ng-repeatتوجيه AngularJS


مثال

اكتب رأسًا واحدًا لكل عنصر في مصفوفة السجلات:

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

التعريف والاستخدام

يكرر 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 snabbköp",
            "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>