How to print an array in table format using angularJS?
Last Updated :
12 Sep, 2022
Given an array & the task is to print the given array in the tabular format using AngularJS.
In JavaScript, data can be stored in the form of arrays. Each of the array items has unique indexing, starting from 0. But what if the developer wants to display all the items that are in the array, on the webpage. One possible way is to run a loop, starting from 0 up to the value of (array.length() - 1). However, this is not feasible while dealing with JSON data, where there might exist another array inside of an already existing array.
The best possible solution for this has been provided by AngularJS. An array can be printed in tabular format using the 'ng-repeat' directive of AngularJS. 'ng-repeat' helps in looping through the items in the collection element. This directive is very helpful while dealing with a collection of objects.
Example 1: This example illustrates the rendering of the array in the tabular form using the ng-repeat directive in AngularJS.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<style>
body {
margin: 2%;
font-size: 120%;
}
th,
td {
padding: 20px;
}
</style>
</head>
<body ng-app="myApp" ng-controller="ListController">
<h1 style="color:green">GeeksforGeeks</h1>
<h3>Printing the array in the tabular format</h3>
<h4>Most Grand Slam Winners - Men Tennis</h4>
<table border=1>
<thead>
<tr>
<th>S.No</th>
<th>Name</th>
<th>Country</th>
<th>Grand Slams</th>
<th>Active</th>
</tr>
</thead>
<tr ng-repeat="item in itemsDetails">
<td> {{item.sno}} </td>
<td> {{item.name}} </td>
<td> {{item.country}} </td>
<td> {{item.grandslams}} </td>
<td> {{item.active}} </td>
</tr>
</table>
</body>
<script>
var app = angular.module('myApp', []);
app.controller(
'ListController', function ($scope) {
$scope.itemsDetails = [{
sno: 1,
name: 'Roger Federer',
country: 'Switzerland',
grandslams: 20,
active: "Yes",
}, {
sno: 2,
name: 'Rafael Nadal',
country: 'Spain',
grandslams: 18,
active: "Yes",
}, {
sno: 3,
name: 'Novak Djokovic',
country: 'Serbia',
grandslams: 16,
active: "Yes",
}, {
sno: 4,
name: 'Pete Samprass',
country: 'USA',
grandslams: 14,
active: "No",
}, {
sno: 5,
name: 'Roy Emerson',
country: 'Australia',
grandslams: 12,
active: "No",
}
];
});
</script>
</html>
Explanation: The 'ng-app' and the 'ng-controller' of this application have been named 'myApp', and 'ListController' respectively. In the <script>, an array named 'itemsDetails' has been created and stored in the scope variable. The array contains a list of the 'Top 5 Grand Slam winners in Men's Tennis.
Output:
The main objective is to print this data in an array in tabular format. The first step is to create a table using the 'table', 'thead', 'tr', 'td' tags. The table headings are set. The ng-repeat service of AngularJS extracts multiple records from the array.
Syntax of 'ng-repeat':
<div ng-repeat="x in list">
{{x}}
</div>
Example 2: In the below example, the syntax has been modified as ng-repeat="item in itemsDetails. Therefore, each item of the 'itemsDetails' will be identified as an 'item'. The JavaScript dot notation will be used to access information inside the array. For example, to access the information that is stored in 'name', the dot notation 'itemsDetails.name' will be used. The next step is to write that dot notation inside double brackets {{}}, for example; {{itemsDetails.name}}. AngularJS then resolves this expression and returns the desired result. The 'ng-repeat' will run this process in a loop until all the array items are selected and returned in the tabular format.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<style>
body {
margin: 2%;
font-size: 120%;
}
th,
td {
padding: 20px;
}
</style>
</head>
<body ng-app="myApp" ng-controller="ListController">
<h1 style="color:green">GeeksforGeeks</h1>
<h3>Printing the array in the tabular format</h3>
<h4>Superheroes and their Universe</h4>
<table border=1>
<thead>
<tr>
<th>Name</th>
<th>Universe</th>
</tr>
</thead>
<tr ng-repeat="item in itemsDetails">
<td> {{item.name}} </td>
<td> {{item.universe}} </td>
</tr>
</table>
</body>
<script>
var app = angular.module('myApp', []);
app.controller('ListController', function ($scope) {
$scope.itemsDetails = [{
name: 'Batman',
universe: 'DC',
}, {
name: 'Ant Man',
universe: 'Marvel',
}, {
name: 'Superman',
universe: 'DC',
}, {
name: 'Captain America',
universe: 'Marvel',
}, {
name: 'Thor',
universe: 'Marvel',
}
];
});
</script>
</html>
Explanation: This example is very much similar to the previous example. In this case, we created an array and named it 'itemsDetails'. We will store this array in the scope object. The 'ng-repeat' service will iterate through the array, get one item at a time from the array, and then display it on the webpage in the tabular format.
Output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read