Angular Pagination Example

Welcome readers, in this tutorial, we will implement pagination in an angular application.

1. Introduction

  • Angular is a Typescript-based open-source framework that helps developers build single page applications
  • Offers Object-oriented features and supports the dynamic loading of the pages
  • Supports Two-way data-binding, Property ([]), and Event (()) binding techniques
  • Supports command-line-interface to easily initiate and manage the angular projects from the command line

Now open the visual studio code and let us see how to implement this tutorial in angular 6 frameworks.

2. Angular Pagination Example

Here is a systematic guide for implementing this tutorial.

2.1 Tools Used

We are using Visual Studio Code and Node Terminal to compile and execute the angular code on a browser.

2.2 Project Structure

In case you are confused about where you should create the corresponding files or folder, let us review the project structure of the angular application.

Angular Pagination - Application
Fig. 1: Application Structure

3. Creating Angular application

Run the ng new angular-routing-child-routes-example command in the npm console to create a new angular project. Once the new project is created, following the below steps.

3.1 Install Pagination module

To start with pagination in an angular application we will install an out-of-box plugin. In the npm console, run the npm install ngx-pagination –save command from the project directory.

3.2 Import Pagination module

To start working with pagination in angular will need to import the NgxPaginationModule module in src/app/app.module.ts file.


// Importing the pagination module for the application.
import { NgxPaginationModule } from 'ngx-pagination';

// Including the pagination module in the application module.
  imports: [
    BrowserModule, NgxPaginationModule

3.3 Initialize Data

Add the following code to the src/app/app.component.ts file to initialize the employee array and the pagination parameters.


import { Component } from '@angular/core';

  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']

export class AppComponent {

  title = 'Angular Pagination Tutorial';
  // Some array of things.
  public employeedata = [];
  // Pagination parameters.
  p: Number = 1;
  count: Number = 5;

  constructor() {
    console.log('Application loaded. Initializing data.');

    this.employeedata = [
      { 'id': 1, 'name': 'Clare Cornau', 'phoneno': '(815) 6180492', 'email': '', 'gender': 'Female', 'nationality': 'Somalia' },
      { 'id': 2, 'name': 'Edouard Elsmore', 'phoneno': '(507) 3119958', 'email': '', 'gender': 'Male', 'nationality': 'United States' },
      { 'id': 3, 'name': 'Aeriel Elldred', 'phoneno': '(478) 7181722', 'email': '', 'gender': 'Female', 'nationality': 'Russia' },
      { 'id': 4, 'name': 'Abagael Meachem', 'phoneno': '(698) 4411762', 'email': '', 'gender': 'Female', 'nationality': 'China' },
      { 'id': 5, 'name': 'Jeremiah Hadwen', 'phoneno': '(345) 6582965', 'email': '', 'gender': 'Male', 'nationality': 'Mongolia' },
      { 'id': 6, 'name': 'Rollin Wainscoat', 'phoneno': '(659) 9557733', 'email': '', 'gender': 'Male', 'nationality': 'Bhutan' },
      { 'id': 7, 'name': 'Micah Braddock', 'phoneno': '(864) 2101861', 'email': '', 'gender': 'Male', 'nationality': 'Peru' },
      { 'id': 8, 'name': 'Jayme Crotty', 'phoneno': '(165) 5814372', 'email': '', 'gender': 'Male', 'nationality': 'Niger' },
      { 'id': 9, 'name': 'Margo Braker', 'phoneno': '(428) 2282928', 'email': '', 'gender': 'Female', 'nationality': 'Argentina' },
      { 'id': 10, 'name': 'Bertie Bosman', 'phoneno': '(673) 5170425', 'email': '', 'gender': 'Female', 'nationality': 'Greece' },
      { 'id': 11, 'name': 'Darelle Rowlands', 'phoneno': '(978) 8885907', 'email': '', 'gender': 'Female', 'nationality': 'Indonesia' },
      { 'id': 12, 'name': 'Neile Keets', 'phoneno': '(956) 9360112', 'email': '', 'gender': 'Female', 'nationality': 'Finland' },
      { 'id': 13, 'name': 'Shari Bussen', 'phoneno': '(240) 7150720', 'email': '', 'gender': 'Female', 'nationality': 'Philippines' },
      { 'id': 14, 'name': 'Arron Drivers', 'phoneno': '(416) 4076124', 'email': '', 'gender': 'Male', 'nationality': 'Bosnia and Herzegovina' },
      { 'id': 15, 'name': 'Carola Balasin', 'phoneno': '(262) 7945277', 'email': '', 'gender': 'Female', 'nationality': 'Bolivia' },
      { 'id': 16, 'name': 'Clarinda Barrick', 'phoneno': '(501) 3984600', 'email': '', 'gender': 'Female', 'nationality': 'China' },
      { 'id': 17, 'name': 'Inglis Treweela', 'phoneno': '(718) 4157883', 'email': '', 'gender': 'Male', 'nationality': 'Finland' },
      { 'id': 18, 'name': 'Yardley Georgeot', 'phoneno': '(213) 5730967', 'email': '', 'gender': 'Male', 'nationality': 'Portugal' },
      { 'id': 19, 'name': 'Hestia Palffrey', 'phoneno': '(349) 6453938', 'email': '', 'gender': 'Female', 'nationality': 'Madagascar' },
      { 'id': 20, 'name': 'Gwendolyn Mordon', 'phoneno': '(474) 3068249', 'email': '', 'gender': 'Female', 'nationality': 'Greece' }

3.4 Create HTML component

Add the following code to the src/app/app.component.html file to display the employee data and include the pagination control.


<!--The content below is only a placeholder and can be replaced.-->
<div class="container">
  <h2 class="text-secondary text-center">{{ title }}!</h2>
  <hr />

    <h4 class="text-info">Displaying employee data.</h4>
    <div> </div>
    <table class="table table-bordered">
          <th>Phone no.</th>
        <tr *ngFor="let e of employeedata | paginate: { itemsPerPage: count, currentPage: p }">

  <!--Including the pagination control.-->
  <div class="text-right">
    <pagination-controls (pageChange)="p = $event"></pagination-controls>

4. Run the Application

As we are ready with all the changes, let us compile and run the angular application with ng serve command. Once the projects are successfully compiled and deployed, open the browser to test it.

5. Project Demo

Open your favorite browser and hit the angular application url (http://localhost:4200/) to display the index page of the application.

Angular Pagination - User Interface
Fig. 2: User Interface

The page displays employee data and pagination control. That is all for this tutorial and I hope the article served you whatever you were expecting for. Happy Learning and do not forget to share!

6. Conclusion

In this section, we learned how to include a simple pagination control in the angular application. Developers can download the sample application as an Eclipse project in the Downloads section.

7. Download the Eclipse Project

This was a tutorial of Pagination in an angular application.

You can download the full source code of this example here: Angular Pagination Example

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
2 years ago

Minor change to the angular 11 version, just change the type from Number to number in the component.ts file

Back to top button