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.