AngularJS(七)迭代1

轉載出處:http://www.cnblogs.com/liulangmao/p/3716307.html


本篇介紹angular中元素的迭代:

複製代碼
<!DOCTYPE html>
<html ng-app>
<head>
  <title>4.1.迭代</title>
  <meta charset="utf-8">
  <script src="../angular.js"></script>
  <script src="script.js"></script>
</head>
<body>
  <ul ng-controller="StudentList">
    <li ng-repeat="student in students">
      <span class="index">{{$index+1}}</span><span class="name"><a href="/student/view/{{student.id}}" class="name">{{student.name}}</a></span><span
            class="score">{{student.score}}</span>
    </li>
  </ul>
</body>
</html>
複製代碼
function StudentList ($scope){
    $scope.students = [{"name":"code_bunny","score":"100","id":"001"},{"name":"white_bunny","score":"90","id":"002"},{"name":"black_bunny","score":"80","id":"003"}]
}

 

ng-repeat="student in students

使用ng-repeat屬性來迭代當前元素,其中
in 之後的
students,是當前作用域下的students變量
in之前的student是自己取的名字,作爲下面的{{}}中當前被循環到的數據的名字.
$index是迭代的索引值,表示當前迭代到第幾條了.從0開始
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章