Exploring the Behavior of Subscribing to a Shared Service in Angular

Within my Angular project, I have implemented a service to share variables across different components. The code for this service is as follows:

import { Injectable } from "@angular/core";
import { BehaviorSubject } from "rxjs";
@Injectable()
@Injectable({
  providedIn: "root"
})

/**
 * Service to manage global variables for use in multiple components
 */
export class SharedService {

  // Global variable for the path 
  private rPathSource = new BehaviorSubject(""); // Set up the source
  currentRPath = this.rPathSource.asObservable(); // Make it Observable

 constructor() {}

  /**
   * Function to update the global path from a component
   * @param path string representing the path of the R Folder for the ML result
   */
  changeRPath(path: any) {
    this.rPathSource.next(path);
  }
}

In one of my components, I subscribe to the shared variable like so: Component 1

constructor(private shared: SharedService) {}

ngOnInit() {
    this.shared.currentRPath.subscribe(RPath => {
      this.currentRPath = RPath;
      // HERE I MAKE A GET REQUEST
    });
  }

From another component, I change the variable using: Component 2

this.shared.changeRPath("");

My project includes a sidenav bar with buttons that change the URL and load different components using ng content.

<ng-content></ng-content>

When I click the button to navigate to component 1, the variable is subscribed to and the get request is made as expected. However, when I click the button to navigate to component 2, the shared variable changes and triggers the get request again, even though component 1 is no longer displayed. It seems that component 1 should be destroyed when the component changes, but this is not happening.

Answer №1

One important thing to remember is to always unsubscribe in order to prevent memory leaks caused by dangling subscriptions.

Here are two methods to achieve this:

  1. Utilize the takeUntil operator:

    export class MyComponent implements OnDestroy, OnInit {
      private readonly destroyed = new Subject<void>();
    
      constructor(private readonly shared: SharedService) {}
    
      ngOnInit() {
        this.shared.currentRPath.pipe(takeUntil(this.destroyed)).subscribe(/*...*/);
      }
    
      ngOnDestroy() {
        this.destroyed.next(undefined);
        this.destroyed.complete();
      }
    }
    
  2. Directly unsubscribe (useful for single subscriptions):

    const mySubscription = this.shared.currentRPath.subscribe(/*...*/);
    mySubscription.unsubscribe(); // when finished.
    

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

Retrieve information from two distinct observables from within the resolver function

I'm facing an issue with my services that work on the observable principle. I am trying to fetch the results of 2 services inside a resolver to display on my page. However, the data being displayed on the page is just an empty data object. I even trie ...

Issues with Angular route links not functioning correctly when using an Array of objects

After hard coding some routerLinks into my application and witnessing smooth functionality, I decided to explore a different approach: View: <ul class="list navbar-nav"></ul> Ts.file public links = [ { name: "Home&quo ...

Is there a way to make PrismaClient return DateTime fields as Unix timestamps rather than JavaScript date objects?

When utilizing the PrismaClient for database interaction, DateTime fields are returned as JavaScript Date objects instead of Unix timestamp numbers. Despite being stored as Unix timestamp numbers in the database itself, I require the dates to be retrieved ...

Develop an Angular application with a customized URL path that utilizes a ServiceWorker

I am currently working on an Angular 10 application. It utilizes the Angular "service worker" to transform into a PWA. Once the app is compiled, it resides in "c:\-website-folder-\ng-app\dist". Users can access the app through a URL like: ...

Launching a website by running ng serve on an EC2 instance running Ubuntu 16.04

I have been trying to work on this Git project, but I'm facing issues in getting the website to function properly on my server. Oddly, everything seems to be working fine on my Mac. Despite not encountering any error messages, I am unable to access t ...

What is the reason for the regeneration of the 2D array?

I have a method called generateWeights() that retrieves random values in an array; And a method named learn() that calls the changeWeights() method to alter the values in the array. Expected: Prior to invoking the learn() method, I anticipate receiving an ...

Using multiple where conditions in TypeORM

SELECT * FROM clients WHERE preferred_site = 'techonthenet.com' AND client_id > 6000; Is there a way to execute this in the typeorm query builder? ...

Exploring the Connection with JSON-server

While creating a simulated API using json-server, I encountered an issue with passing a query. When utilizing _expand, I am able to display the parameters of a relationship, but it doesn't seem to work when the relationship is nested within a child. ...

Angular Resolvers - Achieving Array Success Without Observables. What's the secret behind this accomplishment?

When implementing resolvers in Angular routing, the official documentation suggests using objects in the routing configuration and utilizing an observer in the component to access the resolved data from the activated route. However, I came across a differ ...

How to format decimals in Typescript/Angular with a pipe: comma or dot?

I've recently developed a custom pipe and I'm looking to enhance it by adding commas or periods to thousands values. For instance, 1000 should be displayed as either 1,000 or 1.000. Here is the code snippet for my custom pipe: import { Pipe, Pi ...

Exporting and importing modules in Angular 1 using Webpack 2

Seeking clarification on why the current implementation is not working as expected. It seems like a simple oversight, especially since it works fine without Webpack. I prefer to stick with the current implementation, where each component/controller/etc is ...

Show blob file as a PDF document in a popup or dialog box using HTML and TypeScript

I am currently working on integrating TypeScript and HTML to showcase the result of a webservice call as a PDF in a popup/dialog within the same page. While I can successfully open the PDF in a new tab using the window.open(url) method, I'm encounter ...

When should you utilize child or nested states in AngularJS UI-Router?

I'm currently working on a large AngularJS application that follows the design of having one .html file per state. Each view is represented by a single HTML file, and I am avoiding including multiple HTML files per page. $stateProvider.state('fi ...

Angular 5 is unable to access the value of a form control when the name attribute is not specified

Snippet of HTML code: <li class="dropdownfilter" *ngIf="this.arr.inclues('Male')" (click)="getValueGender('Male',1,)" [(ngModel)]="M"><a>Male</a></li> I encountered the following error: ERROR Error: No value a ...

Tips for managing onChange events in TypeScript

I'm still learning Typescript and I have a question regarding handling the onChange event in a TextField component when using Typescript. Can you provide guidance on how to approach this? I currently have a function called handleChangeDate(e: React. ...

Function parameter constrained to a specific property of a generic type T

In my codebase, I have a custom function called uniqBy, which filters out duplicate items based on a specified key: export function uniqBy<T>(array: T[], key: any): T[] { const seen = {}; return array.filter(function (item) { if (item ...

Combine VS2015 preview with TypeScript 1.3 at your own risk - it may result

After installing VS2015 Preview, I decided to create an MVC project and add a new TypeScript file. However, when trying to compile, I encountered the following error message: The "TypeScript.Tasks.VsTsc" task could not be loaded from the assembly C:&bso ...

Is there a way to ensure that in React (Typescript), if a component receives one prop, it must also receive another prop?

For instance, consider a component that accepts the following props via an interface: interface InputProps { error?: boolean; errorText?: string; } const Input = ({error, errorText}: InputProps) => { etc etc } How can I ensure that when this com ...

Exploring the capabilities of Angular 6 with web components

Encountering a particular issue, I have an Angular 6 application with a routing system in place. My goal is to create a web component that encapsulates this application for usage within another web application. Following a tutorial, I made modifications ba ...

Is it possible to eliminate the table borders and incorporate different colors for every other row?

Eliminating the table borders and applying color to alternate rows. Check out my code snippet: https://stackblitz.com/angular/dnbermjydavk?file=app%2Ftable-overview-example.ts. ...