I created a behavior subject that I am using to toggle a loading spinner icon within my application.
Service:
// Observe our loader status
public loaderStatus: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
/**
* Toggle the loading indicator status
* @param value
*/
displayLoader(value: boolean) {
this.loaderStatus.next(value);
}
Component:
this._massEmpService.displayLoader(true); // Toggle true/false
HTML:
<div *ngIf="objLoaderStatus" class="loader" align="center">
<img src="images/loading-bars.svg" alt="" />
</div>
While this works just fine for a single spinner instance, if I want to use this spinner in multiple areas through my application, the function is too broad and would end up triggering all the spinner instances in the app if multiple spinners existed on the same page.
My Question:
Is it possible to pass an object or multiple params to a behavior subject so that I can not only pass the enabled/disabled
status but also an element ID of some type so I can control which spinner I want to show.
Example Goal:
<div *ngIf="objLoaderStatus && spinnerID == 'home'" class="loader" align="center">
<img src="images/loading-bars.svg" alt="" />
</div>
<div *ngIf="objLoaderStatus && spinnerID == 'search'" class="loader" align="center">
<img src="images/loading-bars.svg" alt="" />
</div>
Function Call:
this._massEmpService.displayLoader(true, 'search');
Whats the best way to go about doing this? Will I need to make a second behavior subject just to hold the spinner's elementID
I want to reference?