AngularJS 迴圈讀取資料 ng-repeat
在AngularJS內要寫迴圈也很方便,準備類似下面的html
- <div ng-controller="PhoneListCtrl">
- <ul>
- <li ng-repeat="phone in phones">
- {{phone.name}}
- <p>{{phone.snippet}}</p>
- </li>
- </ul>
- </div>
在controller內放一個json格式的data
- function PhoneListCtrl($scope) {
- $scope.phones = [
- {"name": "Nexus S",
- "snippet": "Fast just got faster with Nexus S."},
- {"name": "Motorola XOOM™ with Wi-Fi",
- "snippet": "The Next, Next Generation tablet."},
- {"name": "MOTOROLA XOOM™",
- "snippet": "The Next, Next Generation tablet."}
- ];
- }
這樣就可以把data用迴圈跑出來到html內了,真的很方便。
另外想知道json的資料筆數,可以這樣寫
- <p>Total number of phones: {{phones.length}}</p>
Demo
參考資料
留言