AngularJS教程
/ 表格
表格
在表格中顯示數(shù)據(jù)
使用 AngularJS 顯示表格非常簡單:
AngularJS 實例
<div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("customers.php") .then(function (response) {$scope.names = response.data.records;}); }); </script>運(yùn)行實例 ?
點擊 "運(yùn)行實例" 按鈕查看在線實例
使用 CSS 樣式顯示
為了讓它看起來更漂亮,可以向頁面添加一些 CSS:
CSS 樣式
<style> table, th , td { border: 1px solid grey; border-collapse: collapse; padding: 5px; } table tr:nth-child(odd) { background-color: #f1f1f1; } table tr:nth-child(even) { background-color: #ffffff; } </style>運(yùn)行實例 ?
點擊 "運(yùn)行實例" 按鈕查看在線實例
使用 orderBy 過濾器顯示
要對表進(jìn)行排序,請?zhí)砑?orderBy
過濾器:
AngularJS Example
<table> <tr ng-repeat="x in names | orderBy : 'Country'"> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table>運(yùn)行實例 ?
點擊 "運(yùn)行實例" 按鈕查看在線實例
使用 uppercase 過濾器顯示
要以大寫形式顯示,請?zhí)砑?uppercase
過濾器:
AngularJS Example
<table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td>{{ x.Country | uppercase }}</td> </tr> </table>運(yùn)行實例 ?
點擊 "運(yùn)行實例" 按鈕查看在線實例
顯示表索引 ($index)
要顯示表索引,請?zhí)砑訋в?$index
的 <td>:
AngularJS Example
<table> <tr ng-repeat="x in names"> <td>{{ $index + 1 }}</td> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table>運(yùn)行實例 ?
點擊 "運(yùn)行實例" 按鈕查看在線實例
使用 $even 和 $odd
AngularJS Example
<table> <tr ng-repeat="x in names"> <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Name }}</td> <td ng-if="$even">{{ x.Name }}</td> <td ng-if="$odd" style="background-color:#f1f1f1">{{ x.Country }}</td> <td ng-if="$even">{{ x.Country }}</td> </tr> </table>運(yùn)行實例 ?
點擊 "運(yùn)行實例" 按鈕查看在線實例