AngularJS Basic ng repeat Simple Example
- 2021-07-07 06:30:30
- OfStack
AngularJS ng-repeat Directive
AngularJS instance
Loop out multiple titles:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<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 = [
" Rookie tutorial 1",
" Rookie tutorial 2",
" Rookie tutorial 3",
" Rookie tutorial 4",
]
});
</script>
</body>
</html>
Definition and usage
The ng-repeat instruction is used to loop out the HTML element a specified number of times.
Collection must be an array or an object.
Grammar
<
element ng-repeat="expression"
>
<
/element
>
All HTML elements support this instruction.
Parameter value
值 | 描述 |
---|---|
expression | 表达式定义了如何循环集合。 表达式实例规则: x in records (key, value) in myObj x in records track by $id(x) |
More examples
AngularJS instance
Output a table using an array loop:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp">
<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",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"Country" : "Mexico"
},
{
"Name" : "Ernst Handel",
"Country" : "Austria"
}
]
});
</script>
</body>
</html>
Run results:
Alfreds Futterkiste | Germany |
Berglunds snabbk | Sweden |
Centro comercial Moctezuma | Mexico |
Ernst Handel | Austria |
AngularJS instance
Output a table using an object loop:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp">
<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>
</body>
</html>
Run results:
Name | Alfreds Futterkiste |
Country | Germany |
City | Berlin |
The above is the basic data collation of AngularJS ng-repeat instruction, which will be supplemented in the future.