angularjs ng-repeat指令

      对于遍历一个数组或对象已经不陌生了,而在angularjs中也是有遍历的方法,即ng-repeat指令。

一、遍历数组

直接给例子:

a:

<ul><li ng-repeat =' number in [1,2,3]'>{{number}}</li><ul>

 这样输出的结果就是依次输出1,2 ,3

b:

<!DOCTYPE html>
<html lang="en" ng-app = "myapp"  >
<head></head>
<body>
   <div ng-controller = "mycontroller">
   <button ng-repeat = "person in persons">
  {{person.name}}
  {{person.age}}
  {{ $index}}
   </button>
  </div>
</body>

 对应的controller

angular.module('myApp',[])  
        .controller(‘mycontroller’, function($scope) {  
            $scope.persons=
 [{name:'张三',age:'22'},{name:李四,age:34},{name:'王五',age:'20'}];  
})

 会依次在页面上显示三个button,上面依次为0 张三 22 ,1 李四 34 ,2 王五 20

$index为数组的索引

猜你喜欢

转载自jinhonglin001.iteye.com/blog/2260691