The most effective method for implementing a loading or busy modal in Angular 2

Just a small inquiry - what is the most effective way to implement a loading/busy modal that can be used across multiple screens in your application? Would it be better to create a component with ngStyle to display it, and should you include the logic for displaying it on every HTTP request or can it be simplified by using it once in your httpService?

Answer №1

For a seamless implementation like this, my suggestion is to create a component paired with a service. The component will handle the visual aspect - you have the freedom to style it as desired. However, the crucial part lies in the service which exposes a simple boolean/Observable that the component uses to determine its visibility (via ngIf directive).

This setup allows you to provide the loading service at the root injector level, making it accessible to any other component or service requiring the loading indicator. By toggling the boolean value within your service, you can easily control when the loading indicator is displayed or hidden.

It's a straightforward and organized approach.

An example of how the service might be structured:

export class LoadingIndicator {
  private _isLoading = false;

  isLoading(): boolean {
    return this._isLoading;
  }

  show(): void {
    this._isLoading = true;
  }

  hide(): void {
    this._isLoading = false;
  }
}

The LoadingIndicatorComponent would then bind to the isLoading() method. (Alternatively, you could directly use the property -though I personally prefer encapsulating things from my Java background, for simplicity sake, direct usage works fine here.)

To ensure the service injects into the root constructor, I recommend providing it in the AppModule, guaranteeing a single instance throughout the application.

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

Error encountered by Angular's Injector in the AppModule when attempting to access the HttpHandler component

I have been successfully running an app for the past few months with no issues. Now, I am exploring the idea of moving some common services into a library that can be utilized by other applications. For this project, I decided to avoid using Angular CLI t ...

Is it considered acceptable to employ an endless loop to continually monitor the connection to a server?

Today marks the beginning of my journey in creating an Angular App. My goal is to establish a connection to a server and display a message on screen if the connection is offline, prompting the user to check their network settings. I currently have a JavaSc ...

Show all span elements in a map except for the last one

Within my ReactJS application, I have implemented a mapping function to iterate through an Object. In between each element generated from the mapping process, I am including a span containing a simple care symbol. The following code snippet demonstrates t ...

Effortless JSON parsing with Angular 2 Http GET request

After sending an HTTP get request to my server API, I am attempting to parse the JSON object that is returned. Below is the code snippet for the Http call: getPayoutReport(param1, param2, param3) { //perform necessary actions //set up a requestUr ...

Error message "Cannot find children property on type IntrinsicAttributes & RefAttributes<unknown>" occurring in a React component due to a Typescript issue

Issue: The specified type '{ children: string; severity: string; sx: { width: string; }; }' is not compatible with the type 'IntrinsicAttributes & RefAttributes'. The property 'children' is missing in the type 'Intri ...

After the initial test is executed, Jasmine's spy-on function proceeds to call the actual function

The issue arises when the second test fails due to an error message stating that "Cannot read property 'get' of undefined". This error occurs because the second test references the real service, which contains a private property called "http" tha ...

Exploring the power of Angular CLI and webpack

Exploring the capabilities of angular cli https://github.com/angular/angular-cli#documentation After creating a basic app, I can easily access it on localhost. Upon inspecting the localhost site, I notice the css and js links that are added by webpack. ...

Implementing a PhysicsImpostor feature that flips meshes upside-down

After exporting a mesh from Blender and loading it from a GLB file, I encountered an issue with the PhysicsImpostor causing the entire model to flip upside down. Can anyone help me troubleshoot this problem? export class Player extends BABYLON.AbstractMes ...

Exploring Angular 5 Localization

I am new to the concept of locales. I recently created an Angular 4 app that reads the locale from the browser using the navigator.language() API and provides it to Angular's pipes. However, with the changes in v5, I have some questions regarding migr ...

Accessing collection values from referenced document IDs in Firestore---I have provided a unique version of the text

I have two fire store collections with the following reference images: https://i.sstatic.net/QVJkZ.pnghttps://i.sstatic.net/0QFRi.png. I am trying to retrieve the firstName and title from these collections. The signup_id is referenced from the document id ...

What is the best way to implement an Angular Guard that utilizes an API service for validation and redirects in case of failure?

Hello there! I am currently working on an Angular 7 application that deals with time cards. One of the main features I have implemented is a CanActivate Guard for controlling access to certain components. The CanActivate code utilizes Observables to decid ...

What impact does introducing a constraint to a generic type have on the inference process?

Let's take a look at this scenario: function identity<T>(arr: T[]) { return arr } identity(["a", "b"]) In the above code snippet, the generic type T is inferred as string, which seems logical. However, when we introduce a ...

Angular formarray radio buttons not toggling properly in the UI

I have a FormArray that iterates through several test steps. Next to each test step, I am trying to include a radio button group with PASS or FAIL options. When I choose a radio option, it works fine. However, when I select PASS or FAIL on a different row, ...

What is the best way to extract values from a TypeORM property decorator?

import { PrimaryColumn, Column } from 'typeorm'; export class LocationStatus { @PrimaryColumn({ name: 'location_id' }) locationId: string; @Column({ name: 'area_code', type: 'int' }) areaCode: number; } I& ...

Ways to initiate update notification when altering an array object?

I am working on my Angular4 app and I have a component that uses a *ngFor directive to iterate over an array: <div *ngFor="let person of persons"> {{person.name}} {{person.car}} </div> Within the same component, there is a feature to ...

Utilize Typescript to ensure uniformity in object structure across two choices

Looking to create a tab component that can display tabs either with icons or plain text. Instead of passing in the variant, I am considering using Typescript to verify if any of the icons have an attribute called iconName. If one icon has it, then all othe ...

Angular 2 404 Error persists despite successful retrieval of data from Oracle database using Backend Nodejs URL entered directly into the browser

Recently, I've been working on displaying data in my Angular frontend that is fetched from an Oracle DB connected to my Node backend. When I access the physical API link, the data appears and is displayed in the backend console.log. I'm wonderin ...

Protractor encounters difficulty locating a class name

When attempting to select an object by className and not CSS or ID, I keep encountering an error stating that the CSS selector cannot be located, even though I am trying to select by className. The error also mentions that the element cannot be found using ...

Angular CLI integrated with Isotope version 2

I am facing difficulties when using the isotope-layout module with Angular CLI. To install the module, I used the command: npm install isotope-layout --save After installation, I added the script in my .angular-cli.json file: "scripts": [ ... " ...

A guide on getting a mongoose document back from a function with TypeScript

There are two routes in my code that perform the same operation on a token to extract a user document from the database. Subsequently, each route carries out unique operations on this extracted document. In an effort to streamline the code, I am attempting ...