Creating a loading spinner with the help of an rxjs BehaviorSubject

After creating a loading spinner component for my angular 4 application to display during AJAX calls, I encountered an issue when trying to implement it with a subscription to a BehaviorSubject.

This question is similar to how to show a spinner until data is received from the server in angular2, but it's not identical as I intend to use a reusable component. I also came across Angular 2 + RxJS BehaviorSubject subscribe call not working, although I'm struggling to identify any differences between my code and the accepted answer. Could there be something I missed?

The approach I followed is based on this tutorial:

Below are snippets of my code:

app.modules.ts

import {AppComponent} from './app.component';
import {AppRoutingModule} from './app-routing.module';
import {SomeComponent} from './some.component';
import {LoaderComponent} from './loader/loader.component';
import {LoaderService} from './loader/loader.service';

@NgModule({
    declarations: [
        AppComponent,
        LoaderComponent,
        SomeComponent
    ],
    imports: [
        BrowserModule,
        NoopAnimationsModule,
        AppRoutingModule
    ],
    providers: [
        LoaderService
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
}

app.component.html

<router-outlet></router-outlet>
<app-loader></app-loader>

loader.component.html

<div class="loader" *ngIf="isLoading"></div>

loader.component.ts

import {Component, OnInit} from '@angular/core';
import {LoaderService} from './loader.service';

@Component({
    selector: 'app-loader',
    templateUrl: 'loader.component.html',
    providers: [LoaderService]
})

export class LoaderComponent implements OnInit {
    isLoading: boolean;

    constructor(private loaderService: LoaderService) {
    }

    ngOnInit() {
        this.loaderService.status.subscribe((val: boolean) => {
            this.isLoading = val;
        });
    }
}

loader.service.ts

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

@Injectable()
export class LoaderService {
    public status: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

    display(value: boolean) {
        console.log('LoaderService.display ' + value);
        this.status.next(value);
    }
}

A method in some service that does an AJAX call

constructor(private http: Http, private loaderService: LoaderService) {
}

getSomeStuff(): Observable<SomeItem[]> {
    // show the loading spinner
    this.loaderService.display(true);

    const content = this.http.get(this.apiUrl)
        .map(this.extractData);

    content.subscribe(
        () => {
            // hide the loading spinner
            this.loaderService.display(false);
        }
    );

    return content;
}

The main issue is that the loader never appears because the isLoading property is never set to true. The console output shows:

LoaderComponent subscription false
LoaderService.display true
LoaderService.display false

According to how BehaviorSubject should function, the subscription to LoaderService.status within loader.component.ts ought to trigger whenever the status changes. Why is it not working as expected? What am I missing here?

Answer №1

The issue arises from the following line within the LoaderComponent:

providers: [LoaderService]

By declaring the service at the component level, Angular understands that the component should have its own unique instance of the service. However, in this case, you actually want to utilize the instance provided at the module level.

To resolve this, simply delete the aforementioned line from the LoaderComponent so that a single instance of the service is shared throughout.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for adding local images to FormData in Expo version 48 and above?

When working with Expo v47 and its corresponding React Native and TypeScript versions, FormData.append had the following typing: FormData.append(name: string, value: any): void An example of appending images using this code could be: const image = { uri ...

Transfer the view encapsulation id selector to a CSS class within an @Input directive passed down from the parent component

Alright, we all know that using ng-deep is not ideal and that ViewEncapsulation.None can lead to messy code. So here's the dilemma I'm facing. Let's say I have a component with the following; @Input() cssClasses: string; Now, when I use t ...

Retrieving Identifiers with Typescript from an object

I'm a newcomer to Typescript and I'm looking to extract ids from an observable Here's the observable data: let myObj = [{ "id": 1, "text": "Mary" }, { "id": 2, "text": "Nancy" }, { "id": 3, "text": "Paul" }, { "id": 4, "tex ...

Show a few values as a string in Angular using Typescript

I am working with JSON data and I want to know how to display hobbies without including the word "all" in an Angular/TypeScript application. { "Name": "Mustermann1", "Vorname": "Max1", "maennlich& ...

Issue encountered with executing `ng build` in Angular Bitbucket Pipeline

I have set up a pipeline in Bitbucket to deploy my Angular app to an FTP server. The pipeline.yml file for this setup looks like this: image: node:6.9.4 # we need node image to run our angular application in clone: # help to clone our source here de ...

Error due to PlatformLocation's location dependency issue

My AppComponent relies on Location (from angular2/router) as a dependency. Within the AppComponent, I am using Location.path(). However, when running my Jasmine test, I encountered an error. Can you help me identify the issue with my Jasmine test and guide ...

Encountering the error message "TypeError: Cannot access property 'Token' of undefined" while compiling fm.liveswitch

The fm.liveswitch JavaScript Software Development Kit (SDK) is designed for use with both clients and your own backend "app server". It functions smoothly in the frontend thanks to webpack and babel. However, the same import statement: import liveswitch fr ...

Lock the table header in place as the table content scrolls upward using Angular4

Is there a way to keep the table header fixed in place while the rest of the content scrolls up? Below is a snippet from my .html file: <table class="table sticky-header"> <thead> <tr class="row-hor- ...

Generate a fresh array by filtering objects based on their unique IDs using Angular/Typescript

Hey there, I am receiving responses from 2 different API calls. Initially, I make a call to the first API and get the following response: The first response retrieved from the initial API call is as follows: dataName = [ { "id": "1", ...

A convenient utility for generating React components with pre-populated Tailwind CSS classes

When it comes to extracting local Tailwind-styled components, I tend to do it like this: const Container: React.FC = ({ children }) => ( <div className="bg-white px-5 py-5"> {children} </div> ); To simplify this process, I ...

Misunderstanding the concept of always being right

Here is a code snippet that raises an error in TypeScript: class Status { constructor(public content: string){} } class Visitor { private status: Status | undefined = undefined; visit(tree: Tree) { if (tree.value > 7) { this.status = new ...

Using Ionic 3 pipe for filtering lists based on comparison of 2 arrays

Looking for assistance in creating a customized filter PIPE component that can filter and display job postings based on selected search array words. I have been able to return results using one search value, but now need help modifying it to handle multipl ...

Encountered a problem while creating a new Angular 7 app: Module 'temp' not found

While attempting to create a new app using the command ng new first-app, I encountered the following error: internal/modules/cjs/loader.js:589 throw err; ^ Error: Cannot find module 'temp' at Function.Module._resolveFilename (internal ...

Is it feasible to restrict a generic type using typeguard?

I'm currently working on refining a generic function, where the autocomplete feature recognizes that it's encountering a typeguard, preventing it from revisiting the same code block. I suspect that the issue lies in not restricting the type to th ...

Error in Angular 6: String provider is missing from the NullInjector

Parent class import { BadRequestError } from './../common/bad-request-error'; import { NotFoundError } from './../common/not-found-error'; import { AppError } from './../common/app-error'; import { Http } from '@angular/ ...

Error encountered while compiling an Asp.Net Core project due to exceeding the maximum allowable path length in the

Encountering a critical error during the build process with Visual Studio 2016 update 3 Asp.Net Core. The build is interrupted with the following message: Severity Code Description Project File Line Suppression State Error MSB4018 The "FindC ...

Using custom properties from the Material-UI theme object with custom props in Emotion styled components: a step-by-step guide

I have implemented a custom object called fTokens into the MUI theme using Module augmentation in TypeScript This is how my theme.d.ts file is structured declare module "@mui/material/styles" { interface FPalette { ... } interface FTokens ...

What is the method in AngularJS2 for using TypeScript to inject dependencies into components?

I have been encountering different methods of injecting dependencies into my component and not all of them seem to be working for me. I am curious about the advantages and disadvantages, what the recommended best practices are, and why some methods are not ...

Error: This property is not available on either 'false' or 'HTMLAudioElement' types

When working with React (Next.js) using hooks and typescript, I encountered an issue while trying to create a reference to an Audio element. The error message I received was: Property 'duration' does not exist on type 'false | HTMLAudioEleme ...

Module not found when attempting lazy loading routing in Angular2 RC5

Having some challenges implementing RC5 changes in my app, particularly when it comes to lazy loading modules within the routing. Here's the file structure for the affected files in my app: /app /components/meda/meda.module.ts /components/meda ...