Angular HTTP Loader and Spinner
ng-http-loader is an Angular library that provides an automatic loading spinner or progress bar during pending HTTP requests. It achieves this by leveraging Angular's `HttpClient` interceptor mechanism, displaying a visual indicator whenever an active HTTP request is in progress and hiding it once all requests are complete. The current stable version is 19.0.0, which supports Angular 21. The library maintains a rapid release cadence, with new major versions typically released shortly after corresponding Angular major versions to ensure full compatibility and adopt the latest Angular features, such as standalone components, signals, and zoneless change detection. Key differentiators include its tight integration with `HttpClient`, support for various SpinKit-based loaders, and its proactive adoption of modern Angular APIs, providing a streamlined developer experience for adding visual feedback to asynchronous operations.
Common errors
-
ERROR in Error: Metadata version mismatch for module [...]/angular/node_modules/ng-http-loader/ng-http-loader.module.d.ts, found version x, expected y [...]
cause The installed version of ng-http-loader is incompatible with your current Angular major version.fixInstall the ng-http-loader version that precisely matches your Angular major version, as detailed in the package's compatibility table (e.g., `npm install ng-http-loader@19` for Angular 21). -
NG0901: Some of the providers were not statically analyzable. We cannot determine their dependencies or their type.
cause This error often occurs when trying to use functional interceptors (like `pendingRequestsInterceptor$`) with older Angular module-based provider configurations or incorrect injection tokens.fixEnsure you are using `provideHttpClient(withInterceptors([pendingRequestsInterceptor$]))` in your `ApplicationConfig` for standalone apps, or in your `NgModule`'s `providers` array for module-based apps, and that `@angular/common/http` is correctly imported.
Warnings
- breaking Starting from v17.1.0, ng-http-loader transitioned from `@Input` decorators to Angular signals for component inputs. This aligns with modern Angular practices but requires users to update their input binding syntax if upgrading from older versions.
- breaking Version 17.2.0 deprecated and removed `model()` in favor of `input()`. Applications using `model()` for two-way binding with ng-http-loader components will need to refactor.
- breaking Version 17.3.0 introduced 'zoneless' support, which may impact applications relying on Angular's NgZone for change detection or specific side effects within the loader's lifecycle. While generally an improvement, it might require adjustments in specific edge cases.
- gotcha The `ng-http-loader` library *requires* Angular's `HttpClient` for its functionality. If you are using an older HTTP client (e.g., the deprecated `HttpModule` from `@angular/http`) or not using HTTP requests at all, the spinner will not activate.
- breaking Major versions of `ng-http-loader` are tightly coupled with Angular's major versions. Using a version of `ng-http-loader` that is incompatible with your Angular version will result in runtime errors related to metadata mismatch.
Install
-
npm install ng-http-loader -
yarn add ng-http-loader -
pnpm add ng-http-loader
Imports
- NgHttpLoaderComponent
import NgHttpLoaderComponent from 'ng-http-loader';
import { NgHttpLoaderComponent } from 'ng-http-loader'; - pendingRequestsInterceptor$
import pendingRequestsInterceptor from 'ng-http-loader';
import { pendingRequestsInterceptor$ } from 'ng-http-loader'; - NgHttpLoaderModule
const NgHttpLoaderModule = require('ng-http-loader');import { NgHttpLoaderModule } from 'ng-http-loader';
Quickstart
import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { NgHttpLoaderComponent, pendingRequestsInterceptor$ } from 'ng-http-loader';
import { Component } from '@angular/core';
// app.component.ts
@Component({
selector: 'app-root',
standalone: true,
template: `
<h1>My Angular App</h1>
<ng-http-loader></ng-http-loader>
<router-outlet></router-outlet>
`,
styles: [`h1 { color: #333; }`],
imports: [NgHttpLoaderComponent] // <====== import the component
})
export class AppComponent {
title = 'my-angular-app';
}
// app.config.ts
import { routes } from './app.routes'; // Assuming you have app.routes defined
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(withInterceptors([pendingRequestsInterceptor$])),
],
};
// Example of app.routes.ts (for context)
// import { Routes } from '@angular/router';
// export const routes: Routes = [];