AngularJS Fetch Data From API using HttpClient
Last Updated :
25 Apr, 2025
There is some data in the API and our task here is to fetch data from that API using HTTP and display it. In this article, we will use a case where the API contains employee details which we will fetch. The API is a fake API in which data is stored in the form of a JSON (Key: Value) pair.
API stands for Application Programming Interface, which is a software intermediary that allows two applications to communicate with each other. Angular offers HttpClient to work on API and handle data easily. In this approach HttpClient along with subscribe() method will be used for fetching data. The following steps are to be followed to reach the goal of the problem.
- Step 1: Create the necessary component and application.
- Step 2: Do the necessary imports for HttpClient in the module.ts file.
import { HttpClientModule } from '@angular/common/http';@NgModule({
declarations: [
],
imports: [
HttpClientModule,
],
providers: [],
bootstrap: []
})
- Step 3: Do the necessary imports for HttpClient in the component.ts file.
import { HttpClient } from '@angular/common/http';export class ShowApiComponent implements OnInit {
constructor(private http: HttpClient) {
...
}
}
- Step 4: We get Response from API by passing the API URL in get() method and then subscribing to the URL.
this.http.get('API url').subscribe(parameter)
The Response of the API is stored in a variable from which data can be accessed.
- Step 5: Now data array needs to be shown using HTML. A Table is used in which rows are added dynamically by the size of the data array. For this, rows are created using *ngFor then data is shown from each row.
Prerequisite: Here you will need an API for getting data. A fake API can also be created and data can be stored.
Note: We can use any API link in place of (http://...com).
Example: Here, for example, we already have a fake API that contains employee data that we will fetch
Steps to Display the Data:
- Step 1: Required Angular App and Component(Here show-api component) is created
- Step 2: For using HttpClient for our app, HttpClientModule is imported to app.module.ts
app.module.ts:
JavaScript
import { BrowserModule }
from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule }
from '@angular/common/http';
import { AppRoutingModule }
from './app-routing.module';
import { AppComponent } from './app.component';
import { AddInputComponent }
from './add-input/add-input.component';
import { ShowApiComponent }
from './show-api/show-api.component';
@NgModule({
declarations: [
AppComponent,
ShowApiComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
- Step 3: In the Typescript file of the component(Here show-api.component.ts) import HttpClient. HttpClient helps to render and Fetch Data. The Employee Details API is used to get data. We get Response from API by passing the API url in get() method and then subscribing to the URL. The Response of the API is stored in a variable named li from which the data array is further stored in an array named list here. The list array will help us show the data. A user-defined function is called when the response comes to hide the loader.
show-app.component.ts:
JavaScript
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-show-api',
templateUrl: './show-api.component.html',
styleUrls: ['./show-api.component.css']
})
export class ShowApiComponent implements OnInit {
li: any;
lis = [];
constructor(private http: HttpClient) {
}
ngOnInit(): void {
this.http.get(
'http://...com')
.subscribe(Response =& gt; {
// If response comes hideloader() function is called
// to hide that loader
if (Response) {
hideloader();
}
console.log(Response)
this.li = Response;
this.lis = this.li.list;
});
function hideloader() {
document.getElementById('loading').style.display = 'none';
}
}
}
// The url of api is passed to get() and then subscribed and
// stored the response to li element data array list[] is created
// using JSON element property
- Step 4: Now data array needs to be shown using HTML. A Table is used in which rows are added dynamically by the size of the data array. For this, rows are created using *ngFor then data is shown from each row. In this file, added a loader that loads till the response comes.
show-app.component.html:
HTML
<h1>Registered Employees</h1>
<div class="d-flex justify-content-center">
<div class="spinner-border" role="status">
<span class="sr-only" id="loading">
Loading...
</span>
</div>
</div>
<table class="table" id='tab'>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Position</th>
<th scope="col">Office</th>
<th scope="col">Salary</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let e of lis;">
<td>{{ e.name }}</td>
<td>{{ e.position }}</td>
<td>{{ e.office }}</td>
<td>{{ e.salary }}</td>
</tr>
</tbody>
</table>
Output: In the console, the data array of the response can also be seen which is further used to show data:

Similar Reads
How to fetch the details using ng-repeat in AngularJS ?
In this article, we will see how to fetch the details with the help of the ng-repeat directive in Angular, along with understanding its implementation through the illustrations. AngularJS contains various types of pre-defined Directives, where most of the directives start with ng which denotes Angul
2 min read
How To Use HttpClient in Angular?
In Angular, the HttpClient module is used to make HTTP requests to backend services. It simplifies communication with APIs, allowing developers to interact with RESTful services, send and receive data, and handle responses effectively. This article will guide you through setting up HttpClient, makin
6 min read
How to fetch data from APIs using Asynchronous await in ReactJS ?
Fetching data from an API in ReactJS is a common and crucial task in modern web development. Fetching data from API helps in getting real-time updates dynamically and efficiently. API provides on-demand data as required rather than loading all data. PrerequisitesReact JSFetch data from APIApproachTo
3 min read
How to Create RESTful API and Fetch Data using ReactJS ?
React JS is more than just an open-source JavaScript library, it's a powerful tool for crafting user interfaces with unparalleled efficiency and clarity. One of React's core principles is its component-based architecture, which aligns perfectly with the Model View Controller (MVC) pattern. React com
5 min read
How To Fetch Data From APIs In NextJS?
Fetching data from APIs in Next.js can be done using built-in methods like getServerSideProps, getStaticProps, or client-side fetching with useEffect. This flexibility supports both server-side and static data fetching.Prerequisites:NPM & NodeJSReactJSReact HooksReact RouterApproachTo fetch data
2 min read
Meme Generator App using Angular
Angular has become one of the, most popular frameworks for building dynamic web applications. In this project, we will build a simple yet entertaining Meme generator app using Angular. The app will fetch random memes from a public API and display them to the user. This project will help us understan
5 min read
How to print an array in table format using angularJS?
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 t
4 min read
Quote Generator App Using Angular
A Quote Generator App is a simple application that displays random quotes to users. It is a great project for practising Angular basics such as components, services, data and API integration.Here we develop a simple Quote Generator App using Angular. This application can able to display a new Quote
6 min read
Movie App Using Angular
We will be creating a Movie Search Engine using Angular. This application allows users to search for movies by entering keywords and fetching and displaying a list of movie results in card format. The search engine initially loads a default set of movies to showcase the functionality. Through this p
5 min read
Passing data from Child to Parent Component in Angular
In Angular, passing data from a child component to its parent component involves emitting events. Essentially, the child component emits an event containing the data that the parent component needs to receive. This is typically achieved using Angular's EventEmitter class, where the child component e
3 min read