ng-repeat
directive is perfect for displaying tables. Displaying tables with AngularJS is very easy and simple. Following example states the use of ng-repeat
directive to draw a table. <table>
<tr>
<th>Student Name</th>
<th>Marks</th>
</tr>
<tr ng-repeat = "subject in student.subjects">
<td>{{ subject.name }}</td>
<td>{{ subject.marks }}</td>
</tr>
</table>
<style type="text/css">
table, th , td {
border: 1px solid #0099da;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #0099da;
color:#FFF;
}
table tr:nth-child(even) {
background-color: #F6F6F6;
}
</style>
<!DOCTYPE html>
<html>
<head>
<title>AngularJS (Table) Sample Application</title>
<script type="text/javascript" src="../js/angular.min.js"></script>
<style type="text/css">
table, th , td {
border: 1px solid #0099da;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #0099da;
color:#FFF;
}
table tr:nth-child(even) {
background-color: #F6F6F6;
}
</style>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app = "mainApp" ng-controller = "studentController">
<table border = "0">
<tr>
<th>First name :</th>
<td><input type = "text" ng-model = "student.firstName"></td>
</tr>
<tr>
<th>Last name : </th>
<td>
<input type = "text" ng-model = "student.lastName">
</td>
</tr>
<tr>
<th align="left";>Name : </th>
<th>{{student.fullName()}}</th>
</tr>
<tr>
<th>Subject :</th>
<td>
<table>
<tr>
<th>Name</th>.
<th>Marks</th>
</tr>
<tr ng-repeat = "subject in student.subjects">
<td>{{ subject.name }}</td>
<td>{{ subject.marks }}</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<script type="text/javascript">
var mainApp = angular.module("mainApp", []);
mainApp.controller('studentController', function($scope) {
$scope.student = {
firstName: "V V Ramana",
lastName: "Reddy",
fees:500,
subjects:[
{name:'C Language',marks:73},
{name:'HTML5',marks:95},
{name:'PHP',marks:88},
{name:'JavaScript',marks:80},
{name:'AngularJS',marks:76},
{name:'Java',marks:92}
],
fullName: function() {
var studentObject;
studentObject = $scope.student;
return studentObject.firstName + " " + studentObject.lastName;
}
};
});
</script>
</body>
</html>