I am wondering why my loading spinner is not firing, and I think it may be due to my confusion in regard to my LoadingService at the app level, and my LoadingService as a provider at the page level.
APP LEVEL
app.component.html
<app-header></app-header>
<messages></messages>
<loading></loading>
<router-outlet></router-outlet>
app.module.ts
@NgModule({
declarations: [
...
LoadingComponent,
...
]
app.component.ts
providers: [LoadingService]
LOADING COMPONENT AND SERVICE
loading.component.html
<div class="spinner-container" *ngIf="loadingService.loading$ | async">
<mat-spinner></mat-spinner>
</div>
loading.service.ts
import { Injectable } from "@angular/core";
import { BehaviorSubject, Observable, of } from "rxjs";
@Injectable()
export class LoadingService {
private loadingSubject = new BehaviorSubject<boolean>(false);
loading$: Observable<boolean> = this.loadingSubject.asObservable();
loadingOn() {
this.loadingSubject.next(true);
}
loadingOff() {
this.loadingSubject.next(false);
}
}
PAGE THAT USES THE LOADER
page.component.ts
@Component({
...
providers: [LoadingService]
})
constructor(private loadingService: LoadingService) { }
ngOnInit() {
...
this.reloadParameters();
...
}
reloadParameters() {
this.loadingService.loadingOn();
this.parameters$ = this.adminService.getParameters().pipe(
catchError(err => {
const message = "Could Not Load Parameters";
this.messagesService.showErrors(message);
console.log(message, err);
return throwError(err);
}),
finalize(() => this.loadingService.loadingOff())
);
}
A console.log tells me it hits the loadingOn() method and the loading$ observable is true. But no spinner, even when I comment out the api call which should just trigger the spinner indefinitely. But instead, it doesn't trigger the spinner at all.