AngularJS ng-repeat 指令

AngularJS 參考手冊 AngularJS 參考手冊


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

</body>


定義和用法

ng-repeat 指令用於迴圈輸出指定次數的 HTML 元素。

集合必須是數組或對象。


語法

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

所有的 HTML 元素都支持該指令。


參數值

描述
expression 運算式定義了如何迴圈集合。

運算式實例規則:

x in records

(key, value) in myObj

x in records track by $id(x)

更多實例

AngularJS 實例

使用數組迴圈輸出一個表格:

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

AngularJS 實例

使用對象迴圈輸出一個表格:

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


AngularJS 參考手冊 AngularJS 參考手冊