دستورالعمل ng-repeat AngularJS

تعریف و استفاده

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

Specify how to loop through a collection expression.

Legal Expression Examples:

x in records

(key, value) in myObj

x in records track by $id(x)