AngularJS ng-repeat Directive

Definition and Usage

ng-repeat The directive will repeat a set of HTML a specified number of times.

Each item in the HTML collection will be repeated once.

The collection must be an array or an object.

Note:Each repeated instance will get its own scope, which consists of the current item.

If you have a collection of objects:ng-repeat Directives are very suitable for creating HTML tables, displaying a table row for each object, and displaying a table data for each object property. Please refer to the following example.

Instance

Example 1

Write a title for each item in the records array:

<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

Example 2

Write a table row for each item in the records array:

<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

Example 3

Write a table row for each property of the object:

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

x in records

(key, value) in myObj

x in records track by $id(x)