AngularJS Tables
The ng-repeat directive can display tables perfectly.
Displaying Data in a Table
Using angular to display a table is very simple:
AngularJS Examples
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/angular.js/1.6.3/angular.min.js" script head>
<body>
<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("/try/angularjs/data/Customers_JSON.php") .then(function (result) { $scope.names = result.data.records; }); });
script
Try it »
Deprecation Notice (v1.5)
In v1.5
$http'ssuccessanderrorThe method is deprecated. Usethenmethod instead.If you are using a version below v1.5, you can use the following code:
var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("/try/angularjs/data/Customers_JSON.php") .success(function (response) {$scope.names = response.records;}); });
Customers_JSON.php file code:
Using CSS Styles
To make the page more attractive, we can use CSS in the page:
CSS Styles
Try it »
Using the orderBy Filter
To display in sorted order, you can use orderBy filter:
AngularJS Examples
<
table
<
tr
ng-repeat="x in names | orderBy : 'Country'"
<
td
{{ x.Name }}
<
/td
<
td{{ x.Country }}
<
/td
<
/tr
<
/tableTry it »
Using the uppercase Filter
Use uppercase Filter to convert to uppercase:
AngularJS Examples
<
table
<
tr
ng-repeat="x in names"
<
td
{{ x.Name }}
<
/td
<
td{{ x.Country | uppercase }}
<
/td
<
/tr
<
/tableTry it »
Displaying the Index Number ($index)
To show the index number in a table, you can add it in $index:
AngularJS Examples
<
table
<
tr
ng-repeat="x in names"
<
td
{{ $index + 1 }}
<
/td
<
td{{ x.Name }}
<
/td
<
td{{ x.Country }}
<
/td
<
/tr
<
/tableTry it »
Using $even and $odd
AngularJS Examples
<
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
<
/tableTry it »