LoadingComponent provides a global loading indicator that automatically appears during HTTP requests. It integrates with the LoadingService and LoadingInterceptor to provide seamless loading feedback.
Location
src/app/core/components/loading/loading.component.ts
Component Selector
Properties
loadingService
loading$ observable.
Template Structure
The component template (loading.component.html) displays a backdrop with a spinner:
Features
- Conditional Rendering: Only displays when
loadingService.loading$emitstrue - Async Pipe: Uses Angular’s async pipe to subscribe to the loading observable
- Bootstrap Spinner: Uses Bootstrap’s
spinner-bordercomponent - Accessibility: Includes hidden text for screen readers
Loading Service Integration
The component subscribes to theLoadingService to track loading state:
Loading Service Implementation
TheLoadingService manages a counter of in-flight HTTP requests:
- Request Counter: Tracks multiple simultaneous requests
- BehaviorSubject: Provides immediate current state to new subscribers
- Safe Decrement: Uses
Math.max()to prevent negative counter values - Auto-hide: Only hides when all requests complete
HTTP Interceptor Integration
The loading state is automatically managed by theLoadingInterceptor:
- Request Start: Calls
loadingService.show()before each HTTP request - Request Complete: Calls
loadingService.hide()in thefinalize()operator - Error Handling:
finalize()runs regardless of success or error
Usage Example
Basic Setup
Place the component once at the app root level:Interceptor Registration
Register the loading interceptor in your app configuration:Automatic Loading State
Once configured, loading indicators appear automatically for all HTTP requests:Manual Loading Control
You can also control loading state manually without HTTP requests:Multiple Concurrent Requests
The service handles multiple simultaneous requests correctly:Styling
The loading backdrop typically uses custom CSS for full-screen overlay:Accessibility
The component includes proper ARIA attributes:role="status"on the spinner containervisually-hiddenclass with “Loading…” text for screen readers
Best Practices
- Single Instance: Place only one
<app-loading>component at the root level - Automatic Management: Let the interceptor handle HTTP request loading
- Manual Control: Use
loadingService.show()/hide()for non-HTTP operations - Always Hide: Use try-finally blocks when manually controlling loading state
- Z-Index: Ensure the loading backdrop has a high z-index to overlay all content