AngularJS ng-repeat
지시문
예시
레코드 배열의 각 항목에 대해 하나의 헤더를 작성합니다.
<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>
정의 및 사용
지시문 은 ng-repeat
지정된 횟수만큼 HTML 집합을 반복합니다.
HTML 세트는 컬렉션의 항목당 한 번 반복됩니다.
컬렉션은 배열 또는 개체여야 합니다.
참고: 반복의 각 인스턴스에는 현재 항목으로 구성된 자체 범위가 지정됩니다.
개체 컬렉션이 있는 경우 ng-repeat
지시문은 HTML 테이블을 만들고 각 개체에 대해 하나의 테이블 행을 표시하고 각 개체 속성에 대해 하나의 테이블 데이터를 표시하는 데 적합합니다. 아래 예를 참조하십시오.
통사론
<element ng-repeat="expression"></element>
모든 HTML 요소에서 지원됩니다.
매개변수 값
Value | Description |
---|---|
expression | An expression that specifies how to loop the collection. Legal Expression examples: x in records
(key, value) in myObj |
더 많은 예
예시
레코드 배열의 각 항목에 대해 하나의 테이블 행을 작성합니다.
<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>
예시
객체의 각 속성에 대해 하나의 테이블 행을 작성합니다.
<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>