How to Display Spinner on the Screen till the data from the API loads using Angular 8 ? Last Updated : 07 Mar, 2024 Comments Improve Suggest changes Like Article Like Report The task is to display a spinner on the page until the response from the API comes. Here we will be making a simple CSS spinner which will load till the data from API comes. You can also take bootstrap spinners or can make spinner on your own. Prerequisite: You will need some knowledge for making Http get() requests from API and getting data. Here you will need an API for getting data. A fake API can also be created and data can be used to display. We already have a fake API that contain the following data: Approach: Required Angular App and Component is created. ng new app_name ng g c component_name In component.html file, make an object with id loading. Here spinner is defined as: <div class="d-flex justify-content-center"> <div class="spinner-border" role="status" > <span class="sr-only" id="loading"></span> </div> </div> You can make a spinner your way. In component.css file, give spinner the styles you want. Here spinner is styled as: #loading{ position: absolute; left: 50%; top: 50%; z-index: 1; width: 150px; height: 150px; margin: -75px 0 0 -75px; border: 16px solid #f3f3f3; border-radius: 50%; border-top: 16px solid #3498db; width: 120px; height: 120px; animation: spin 2s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } Fetch the data from API by making get request.After fetching the data from API store it in a Response variable.There is an if statement that checks if Response from API came or not.If Response came then there is a function hideloader() which is called.In that hideloader() function by using DOM manipulation, we set display of loading element to none.document.getElementById('loading').style.display = 'none';For further clarity of getting data, I had shown the fetched data to HTML using interpolation data binding. Code Implementation app.module.ts javascript import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { HttpClientModule } from '@angular/common/http'; import { FormsModule } from '@angular/forms'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { ShowApiComponent } from './show-api/show-api.component'; @NgModule({ declarations: [ AppComponent, ShowApiComponent, ], imports: [ BrowserModule, AppRoutingModule, HttpClientModule, FormsModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { } show-api.component.html html <h1>GeeksforGeeks</h1> <!-- spinnner element is defined with id loading --> <div class="d-flex justify-content-center"> <div class="spinner-border" role="status"> <span class="sr-only" id="loading"></span> </div> </div> <!-- data from API is displayed --> <h2>{{ dataDisplay }}</h2> show-api.component.css javascript #loading{ position: absolute; left: 50%; top: 50%; z-index: 1; width: 150px; height: 150px; margin: -75px 0 0 -75px; border: 16px solid #f3f3f3; border-radius: 50%; border-top: 16px solid #3498db; width: 120px; height: 120px; animation: spin 2s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } show-api.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 { dt: any; dataDisplay: any; constructor(private http: HttpClient) { } ngOnInit(): void { this.http.get( 'http://www.mocky.io/v2/5ec6a61b3200005e00d75058') .subscribe(Response => { // If Response comes function // hideloader() is called if (Response) { hideloader(); } console.log(Response) this.dt = Response; this.dataDisplay = this.dt.data; }); // Function is defined function hideloader() { // Setting display of spinner // element to none document.getElementById('loading') .style.display = 'none'; } } } Output: Run the development server to see the output spinner loads till API data comes CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples. Comment More infoAdvertise with us Next Article How to Display Spinner on the Screen till the data from the API loads using Angular 8 ? taran910 Follow Improve Article Tags : Web Technologies CSS AngularJS AngularJS-Misc Similar Reads 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 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 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 Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of 10 min read Introduction of Firewall in Computer Network A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa 10 min read Like