AngularJS基础 ng-repeat 指令简单示例
AngularJSng-repeat指令
AngularJS实例
循环输出多个标题:
<!DOCTYPEhtml> <html> <head> <metacharset="utf-8"> <scriptsrc="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <bodyng-app="myApp"ng-controller="myCtrl"> <h1ng-repeat="xinrecords">{{x}}</h1> <script> varapp=angular.module("myApp",[]); app.controller("myCtrl",function($scope){ $scope.records=[ "菜鸟教程1", "菜鸟教程2", "菜鸟教程3", "菜鸟教程4", ] }); </script> </body> </html>
定义和用法
ng-repeat指令用于循环输出指定次数的HTML元素。
集合必须是数组或对象。
语法
<elementng-repeat="expression"></element>
所有的HTML元素都支持该指令。
参数值
值
描述
expression
表达式定义了如何循环集合。
表达式实例规则:
xinrecords
(key,value)inmyObj
xinrecordstrackby$id(x)
更多实例
AngularJS实例
使用数组循环输出一个表格:
<!DOCTYPEhtml> <html> <head> <metacharset="utf-8"> <scriptsrc="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <bodyng-app="myApp"> <tableng-controller="myCtrl"border="1"> <trng-repeat="xinrecords"> <td>{{x.Name}}</td> <td>{{x.Country}}</td> </tr> </table> <script> varapp=angular.module("myApp",[]); app.controller("myCtrl",function($scope){ $scope.records=[ { "Name":"AlfredsFutterkiste", "Country":"Germany" }, { "Name":"Berglundssnabbk", "Country":"Sweden" }, { "Name":"CentrocomercialMoctezuma", "Country":"Mexico" }, { "Name":"ErnstHandel", "Country":"Austria" } ] }); </script> </body> </html>
运行结果:
AngularJS实例
使用对象循环输出一个表格:
<!DOCTYPEhtml> <html> <head> <metacharset="utf-8"> <scriptsrc="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> </head> <bodyng-app="myApp"> <tableng-controller="myCtrl"border="1"> <trng-repeat="(x,y)inmyObj"> <td>{{x}}</td> <td>{{y}}</td> </tr> </table> <script> varapp=angular.module("myApp",[]); app.controller("myCtrl",function($scope){ $scope.myObj={ "Name":"AlfredsFutterkiste", "Country":"Germany", "City":"Berlin" } }); </script> </body> </html>
运行结果:
以上就是对AngularJSng-repeat指令的基础资料整理,后续继续补充。