AngularJS ng-repeat 指令

返回上一级

AngularJS ng-repeat 指令用于循环输出指定次数的 HTML 元素

集合必须是数组或对象

语法

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

所有的 HTML 元素都支持该指令

参数值

描述
expression 表达式定义了如何循环集合
表达式范例规则:
x in records(key, value)
x in myObjx
x in records track by $id(x)

范例 1

循环输出多个标题

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

运行范例 »

范例 2

使用数组循环输出一个表格

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

运行范例 »

返回上一级

学习 AngularJS

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.