AngularJS ng-repeatDirective

AngularJS 参考手册AngularJS Reference Manual


AngularJS Example

Loop and output multiple headings:

<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 = [
"Example Tutorial 1",
"Example Tutorial 2",
"Example Tutorial 3",
"Example Tutorial 4",
    ]
});
</script>

</body>

Try it Yourself »

Definition and Usage

ng-repeatThe directive is used to loop and output HTML elements a specified number of times.

The collection must be an array or an object.


Syntax

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

All HTML elements support this directive.


Parameter Values

Value Description
expression The expression defines how to loop through the collection.

Expression example rules:

x in records

(key, value) in myObj

x in records track by $id(x)

More Examples

AngularJS Example

Use an array to loop and output a table:

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

AngularJS Example

Use an object to loop and output a table:

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

AngularJS 参考手册AngularJS Reference Manual

Other Extensions