AngularJS ng-repeat 指示

定義と用法

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>

自分で試してみてください

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

自分で試してみてください

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

自分で試してみてください

文法

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

すべての HTML エレメントがサポートされています。

パラメータ

パラメータ 説明
expression

コレクションをループする方法を指定する表現。

合法な表現の例:

x in records

(key, value) in myObj

x in records track by $id(x)