How to implement ngx-spinner in an Angular http subscribe operation

I'm trying to incorporate a spinner into my Angular Application using ngx-spinner. I've come across this library but haven't found enough practical examples on how to use it effectively. Specifically, I want to integrate the spinner with my http requests. Can someone guide me on how to properly show and hide the spinner in this context?

public getCars(){
 this.spinner.show();
  this.appService.getCars().subscribe(
    car => {
      this.carList=car;
      for (let i = 0; i < this.carList.length; i++) {
        let make = this.carList[i].make;
      }
      this.spinner.hide();
    },
  );
}

I have successfully installed and imported ngx-spinner, however, I am facing challenges in utilizing it within my http requests. Any assistance or suggestions would be greatly appreciated.

Answer №1

In my opinion, the correct placement for "this.spinner.hide();" is directly under the closing brace of the "for" statement. This signifies the success-callback in the code.

Answer №2

Give this a try:

function retrieveCars(){
     this.loading.show();
      this.carService.getCars().subscribe(
        car => {
          this.carList=car;
          for (let i = 0; i < this.carList.length; i++) {
            let brand = this.carList[i].brand;
          }
          this.loading.hide();
        },
      );
    }

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

Efficient communication across angular modules on a global scale

Currently, I am working on a project that involves Angular2 with multiple modules. One of the main modules is called BaseModule, and in addition to that, there are two extra modules - FirstModule and SecondModule. Each module has its own routing mechanis ...

"NODEJS: Exploring the Concept of Key-Value Pairs in Object

I am facing a challenge with accessing nested key/value pairs in an object received through a webhook. The object in req.body looks like this: {"appId":"7HPEPVBTZGDCP","merchants":{"6RDH804A896K1":[{"objectId&qu ...

Avoid using propTypes for props verification

Looking for a solution to handle multiple props on a button: interface buttonProps { secondary?: boolean; tertiary?: boolean; width?: number; children?: any; icon?: string; } If the button includes an icon without any children, how can ...

Clearly define in typescript that a variable should not be null

Encountering an issue with typescript involving a mongoose model that is interface casted. import { Schema, model } from "mongoose"; interface IUser { userId: string guildId: string cash: number bank: number } const userSchema = ...

Sorting an object array by date is causing a problem

UPDATE: Finally cracked the code on this issue. I initially assumed that Date was interpreting the date ("29-04-2020") as DD-MM-YYYY, when it should actually be MM-DD-YYYY. For instance, here's an object array I'm working with: let t ...

What is the method for reaching a static member of a class within a decorator for a method of the same class?

Upon the release of TypeScript 5.0, the new decorator APIs have been introduced and I am eager to utilize them for automatically providing parameters to a method from a static property within the same class. The decorator parameters and factory are defined ...

Why is my Angular Reactive form not retrieving the value from a hidden field?

I've encountered a problem where the hidden field in my form is not capturing the product id unless I manually type or change its value. It consistently shows as "none" when submitting the form. TS https://i.stack.imgur.com/W9aIm.png HTML https://i. ...

When dynamically selecting an item from a dropdown menu, the object property does not display as expected when using the patchValue

When attempting to set the value for a sort object with specific type and format, I encountered an issue where it was not being rendered. Below is my code snippet using patch to set the value: let arr = <FormArray>this.myForm.controls.users; arr.c ...

Updates to TypeScript 2.3.1 creating disruptions in SystemJS plunk

Check out this official Angular + TypeScript plunk using SystemJS 0.19.31, now updated to TypeScript 2.3.0. However, changing the SystemJS configuration in the same plunk to TypeScript 2.3.1 or 2.3.2 'typescript': 'npm:<a href="/cdn-cgi ...

Oops! There was an error: Uncaught promise rejection: TypeError - Unable to access 'subscribe' property of null object (Ionic Angular)

I encountered an issue in my angular ionic project. When I log in, the first page displays the error "ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'subscribe' of null." However, upon reloading the page, the error disappears ...

Access SCSS variable values in Angular HTML or TypeScript files

So, I've been looking into whether it's feasible to utilize the SCSS variable value within HTML or TS in Angular. For instance: Let's say I have a variable called $mdBreakpoint: 992px; stored inside the _variable.scss file. In my HTML cod ...

Error: Component with nested FormGroup does not have a valid value accessor for form control in Angular

In my setup, the parent component is utilizing a FormGroup and I am expecting the child components to notify any changes in value back to the parent. To achieve this, I am trying to implement NG_VALUE_ACCESSOR in the child component so that it can act like ...

I recently downloaded a project from Github, but I'm encountering issues as the npm start command is not

This is the latest update for my project: https://github.com/rietesh/Hyperledgerfabric-Airline-App.git Upon running npm start, the following error message is displayed: The serve command should only be executed within an Angular project, but no project de ...

Integration of NextAuth with Typescript in nextjs is a powerful tool for authentication

I am diving into NextAuth for the first time, especially with all the new changes in Nextjs 13. Setting up nextauth on my project seems to be a daunting task. I have gone through the documentation here I am struggling to configure it for nextjs 13. How do ...

Having trouble with installing Bootstrap in Angular 5

My journey with Bootstrap began by executing the command below: npm install --save bootstrap The installation was a success, and I proceeded to incorporate the CSS as follows: "styles": [ "../node_modules/bootstrap/dist/css/bootstrap.min.css", ...

When iterating through a list of strings using ngFor, I am unable to directly access or manipulate the individual items

Within my model, I have a list of strings. <span *ngFor="let item of model; let i = index"> <input #inputField type="text" [name]="name + '_' + i" [(ngModel)]="item" /> </span> The code snippet ab ...

Validating Angular input for decimal values based on specific criteria

Is there a way to limit user input to numbers with only 2 decimal places if both itemNew.UL_DATA and itemNew.LL_DATA are equal to 0 or blank? <ion-col col-2> <input (keypress)="ShowKey();" [style.display]="itemNew.UL_DATA== ...

Preventing the display of the login page for an authenticated user in Angular

My application has different modules for authentication, dashboard, and root routing. The auth module contains routes for sign-in, the dashboard module has routes for home with authentication guard, and the root module redirects to home or a PageNotFound c ...

VS Code failing to detect Angular, inundated with errors despite successful compilation

Having some issues with loading my angular project in vscode. It used to work fine, but suddenly I'm getting errors throughout the project. I have all the necessary extensions and Angular installed. https://i.stack.imgur.com/qQqso.png Tried troubles ...

Processing authentication result for HTTP Active Directory login

I have been working on integrating Active Directory login for my website. The site is being hosted on a Node.js server that communicates with the AD server through LDAP, even though they are not physically located on the same machine. When attempting to i ...