Steps to create a personalized loading screen using Angular

I am looking to enhance the loading screen for our angular 9 application. Currently, we are utilizing

<div [ngClass]="isLoading ? 'loading' : ''>
in each component along with the isloading: boolean variable.

Whenever an API call is made, we switch the value of this variable to true which triggers a spinning wheel to appear, freezing the app until the API call completes. Once finished, we set back the value to false and display the content to the user.

I am interested in creating a customized loading screen to provide additional information. Does anyone have any suggestions or solutions for implementing this?

Answer №1

Show a loading spinner using the ngx-spinner library when making calls to a back end service:

Here is an example:

Prior to making the back end call, include this line of code:

this.spinner.show();

In your template, you can add the following:

<ngx-spinner [fullScreen]="true" type="ball-clip-rotate-multiple" size="medium" bdColor="rgba(32, 55, 77, 0.85)">
  <p class="loading" ><Strong>Please wait ...</Strong></p>
</ngx-spinner>

Once the service returns results, use this code:

this.spinner.hide();

Take a look at this demo: https://example.com/demo123

For more information, visit: https://example.com/ngx-spinner-demo

Answer №2

When developing my applications, I always make sure to include a loading state component. It's important to cover all aspects of UI components, including:

  • blank state (Content is loaded but empty, such as an empty list)
  • loading state (Content is still loading)
  • partial state (Certain parts of the view are not yet complete or working on certain elements, like a long list with few entries)
  • error state (Something has gone wrong and cannot be recovered)
  • ideal state (Perfect scenario where content is available and well-structured)

In my approach, I create components specifically for error, loading, and blank states, while incorporating ideal and partial states into other components. By providing inputs, these components can adapt based on contextual information. For instance, in a project view that is empty, a message like "You have no projects yet" could be displayed by the empty state component.

To implement this, you can use:

<ng-container *ngIf="isLoading" ><app-loading-state></app-loading-state></ng-container>
<ng-container *ngIf="error" ><app-error-state [reason]="Failed to load"></app-error-state></ng-container>
<ng-container *ngIf="!isLoading" ><!-- Content of your view here. --></ng-container>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to retrieve the previous URL in Angular after the current URL has been refreshed or changed

Imagine being on the current URL of http://localhost:4200/#/transactions/overview/5?tab=2 and then navigating to http://localhost:4200/#/deals/detail/ If I refresh the deals/detail page, I want to return to the previous URL which could be something like h ...

String date in the format "dd-MM-yyyy" cannot be transformed into a date using the 'DatePipe' function

Having trouble with date conversion in my custom pipe. It seems that when using a locale of 'nl-nl' and trying to format the date as 'dd-MM-YYYY', I'm seeing an error message stating Unable to convert "16-02-2023" into a ...

The battle of Any, Generic Type, and Unknown in Typescript

Is it advisable to replace the type any with Generic Types? I understand that using type any is generally discouraged as it removes type checking in TypeScript, making it unsafe. So what is a better alternative - using unknown or generic types? For examp ...

Creating a declaration of an array containing key value pairs in Typescript

Feeling lost with the syntax provided below: constructor(controls: {[key: string]: AbstractControl}, optionals?: {[key: string]: boolean}, validator?: ValidatorFn, asyncValidator?: AsyncValidatorFn) I'm curious about the type of the controls (first ...

The 'Subscription' type does not contain the properties: source, operator, lift, subscribe, and three other properties that are present in the 'Observable<EnumValue[]>' type

Are you struggling with assigning an Observable<EnumValue[]> to another Observable<EnumValue[]>? fetchContactLocationStates() { this.contactLocationStates$ = this.enumValues$ .pipe(takeUntil(this.destroy$)) .subscribe(x => x.fil ...

Error in Ionic Cordova Build prod: Module "." not found - Requires Typescript version >3

After updating my ionic project and all dependencies, I encountered an error when trying to build a --prod android apk: Uncaught Error: Cannot find module "." at vendor.js:1 at vendor.js:1 at Object.<anonymous> (vendor.js:1) at e (vendor.js:1) at Ob ...

Guidelines for displaying user profile information in the dashboard page through an Angular project, considering the different user types (user, super user, admin)

In my application, I have implemented the dashboard feature. There are three types of dashboards: the regular user dashboard, super user dashboard, and admin dashboard. The super user and admin dashboards include additional tables along with the data from ...

React: a versatile and type-specific onChange() function

After adding as HTMLInputElement, the error message of Property 'checked' does not exist on type 'EventTarget & (HTMLInputElement | HTMLTextAreaElement)' is resolved. However, I find it interesting that TypeScript doesn't autom ...

The error message "Error: cannot read property ‘setDirtyAttribute’ of null" may be encountered when attempting to use the method YourModel.create({...}) in ember-typescript-cli

Encountering the error cannot read property 'setDirtyAttribute' of null even when using YourModel.create({...}) in ember-typescript-cli to instantiate an EmberObject. Model: import DS from 'ember-data'; import {computed} from "@ember/ ...

Limit access to a specific route within the URL

Is there a way to ensure that users can access and download myfile.pdf without being able to see or access /root, /folder, or /subdirectory in the URL? This functionality can be implemented using HTML, Angular 6, ReactJS, or similar frameworks. ...

Providing the correct context to the function in Angular's dialog data is crucial for seamless

Currently, I have a scenario where a service and a component are involved. In the service, there is an attempt to open a dialog in which a reference to a method existing in the service is passed. The code snippet below provides an example: export class So ...

Azure pipeline failing to run Visual Studio 2017 task because of outdated Typescript SDK version

The Visual Studio 2017 build encounters an error during the build process src\Service\node_modules\utility-types\dist\aliases-and-guards.d.ts(10,51): Error TS2304: Build:Cannot find name 'bigint This issue is specific to the ...

Subscribing to ngrx store triggers multiple emissions

Currently, I have an app with a ngrx store set up. I am experiencing an issue where, upon clicking a button, the function that fetches data from the store returns multiple copies of the data. Upon subsequent clicks, the number of returned copies grows expo ...

Tips for Verifying a User's Identity using Username and Password

After examining this Angular 2 solution: state: string = ''; error: any; constructor(public af: AngularFire, private router: Router) { this.af.auth.subscribe(auth => { if (auth) { this.router.navigateByUrl('/mem ...

The issue I'm encountering in Angular 2 with Angular Material is the inability to delete specific items from a mat

I successfully created a table in Angular 2 using angular material. I have implemented two methods: transferSelectedRows, which moves selected rows from the table to the Selected Rows section, and removeSelectedRows, which should delete the corresponding l ...

Unable to remove item from store using NgRx is causing issues

I recently started learning NgRx and decided to build a small app for practice. The app consists of two text fields where users can add items to a list, which is then displayed on the screen. While I successfully managed to add items to the list, I encount ...

How come ngOnChange is unable to detect changes in @Input elements when ngOnDetect is able to do so?

Check out this plunker Please note: In order to see the effect, you need to restart the app after entering the link. import {Component, OnInit, Input, OnChanges, DoCheck} from 'angular2/core' @Component({ selector: 'sub', templat ...

TypeScript has two variable types

I'm facing a challenge with a function parameter that can accept either a string or an array of strings. The issue arises when trying to pass this parameter to a toaster service, which only accepts the string type. As a result, when using join(' ...

Ionic 2 Media Plugin File Status with Ionic Native

In the Ionic Native Media plugin documentation found here, it mentions that there are both static and instance members, such as status. I am looking for an example related to the file status in the media plugin. I attempted to do this: console.log(this. ...

Managing copious amounts of data in an Angular project using a Restful API

After developing my own Restful API within a Laravel project, I am faced with the challenge of efficiently managing large amounts of data returned by the API in order to display it on an Angular front-end project. For instance, when using the GET method t ...