Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

AngularJS ng-repeat Directive

AngularJS ng-repeat Instruction

AngularJS Reference Manual AngularJS Reference Manual


AngularJS Examples

Loop to output multiple headings:

<

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

        "Runoob Tutorial1",

        "Runoob Tutorial2",

        "Runoob Tutorial3",

        "Runoob Tutorial4",

    ]

});

<

/script

<

/body

Try it »


Definition and Usage

ng-repeat The directive is used to output an HTML element a specified number of times in a loop.

The collection must be an array or an object.


Syntax

<

element

ng-repeat=

"expression"

<

/element

All HTML elements support this directive.


Parameter value

Value Description
expression The expression defines how to loop through the collection.
 表达式实例规则:       
   
 `x in records`*        
    
 *`(key, value) in myObj x in records track by $id(x)` |

More examples

AngularJS Examples

Use an array to loop and output a table:

<

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

Try it »

AngularJS Examples

Use an object to loop and output a table:

<

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

Try it »


AngularJS Reference Manual AngularJS Reference Manual