AngularJS ng-repeat 指令

AngularJS 參考手冊 AngularJS 參考手冊


AngularJS 實例

循環(huán)輸出多個標題:

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

</body>

嘗試一下 ?

定義和用法

ng-repeat 指令用于循環(huán)輸出指定次數(shù)的 HTML 元素。

集合必須是數(shù)組或?qū)ο蟆?/p>


語法

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

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


參數(shù)值

描述
expression 表達式定義了如何循環(huán)集合。

表達式實例規(guī)則:

x in records

(key, value) in myObj

x in records track by $id(x)

更多實例

AngularJS 實例

使用數(shù)組循環(huán)輸出一個表格:

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

使用對象循環(huán)輸出一個表格:

<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 參考手冊