How can I avoid a component from triggering its subscription twice in Angular 5 when the component is nested twice inside another component?

How can I prevent a component in Angular 5 from triggering its subscription twice when the component is placed twice inside another component? For example, I have a NavMenuComponent with two instances of a cart in its template.

        <!-- cart 1 in nav fixed -->
    <nav-menu-cart class="toolbar-item" *ngIf="auth.isAuthenticated()"></nav-menu-cart>


        <!-- cart 2 in nav scrolled -->
    <nav-menu-cart class="toolbar-item" *ngIf="auth.isAuthenticated()"></nav-menu-cart>

Within the NavMenuCartComponent, there is a subscription in the constructor:

    this.subscription = cartConnectorService.cartItemAdded$.subscribe(
  newCartItem => {
    console.log("in cartConnectorService.cartItemAdded$.subscribe");
    this.saveCartWithNewItem(newCartItem);
  });

The saveCartWithNewItem function gets called twice instead of once (as evidenced by the log screenshot below).

https://i.sstatic.net/FYAlb.png

You might wonder why the NavMenuCartComponent handles the call to saveCartWithNewItem instead of having the data managed within a CartService and just refreshing the data in the NavMenuCartComponent. The reason is that the CartService only deals with HTTP calls, not data storage at the moment.

The ProductsGridComponent, which makes use of the CartConnectorService, is the original component responsible for triggering the Observable.

I aim to allow the ProductsGridComponent to add items to the cart while ensuring that both instances of NavMenuCartComponent receive the updated data and persist it through the service. Although I used the CartConnectorService for this purpose, I now encounter the issue of duplicate invocation. Any assistance would be appreciated.

Answer №1

If you want to enable the NavMenuCartComponent to save the cart when a new item is added, you can set up an @Input for this functionality.

Inside the NavMenuCartComponent:

@Input saveCartOnNewItem: boolean;

//...

ngOnInit() {
    // ...

    if(saveCartOnNewItem) {
        this.subscription = cartConnectorService.cartItemAdded$.subscribe(newCartItem => {
            console.log("in cartConnectorService.cartItemAdded$.subscribe");
            this.saveCartWithNewItem(newCartItem);
        });
    }
}

Then, in your HTML, indicate which cart should be saved:

     <!-- cart 2 in nav scrolled -->
     <nav-menu-cart class="toolbar-item" [saveCartOnNewItem]="true" ngIf="auth.isAuthenticated()"></nav-menu-cart>

Answer №2

One straightforward approach that comes to mind is:

if (!this.subscription) {
  this.subscription = cartConnectorService.cartItemAdded$.subscribe(
    newCartItem => {
      console.log("in cartConnectorService.cartItemAdded$.subscribe");
      this.saveCartWithNewItem(newCartItem);
    });
}

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

Switching the keyboard language on the client side of programming languages

I'm interested in altering the keyboard language when an input element changes. Is it possible to modify the keyboard language using client-side programming languages? And specifically, can JavaScript be used to change the keyboard language? ...

What is a more efficient way to write nested subscribe in Angular?

I am a beginner with RxJS and I'm interested in learning how to write clean code using it. I currently have a nested subscription that I've been trying to refactor without success. firstMethod() { this.testMethod(name) console.log(this.curren ...

DxDataGrid: Implementing a comprehensive validation system for multiple edit fields

I'm currently working with a DxDataGrid within an Angular Application. Within this particular application, I have the need to input four dates. I've implemented validation rules that work well for each individual field. However, my challenge aris ...

Displaying random characters in place of Angular 6 font awesome icons

Recently, I started a new project with the angular cli and incorporated font-awesome 4.7.0. After that, I included it as a dependency in my angular.json file. "styles": [ "./node_modules/font-awesome/css/font-awesome.min.css", "./node ...

Loop through an array using NgFor directive in Angular

I am facing a challenge where I have an object in my front-end application that needs to be iterated over using ngFor. The response received from the backend is structured as follows: @Component({ selector: 'docker-logs', templateUrl: ' ...

Having trouble verifying the selection option in Angular 6

I've been trying to implement Select option validation in Angular 6, but neither Aria-required nor required seem to be effective. The requirement is for it to display a message or show a RED border similar to HTML forms. Here's the HTML snippet ...

The Node Express proxy is returning a 404 Not Found status code instead of the expected 200 success code

Here is the content of my server.js file: const express = require('express'); const http = require('http'); const path = require('path'); //const request = require('request'); const app = express(); var cors = requi ...

Getting started with Angular 2 using NPM version 3.10.6 and Angular CLI 1.0.0

I am having trouble when I run 'NPM start,' all I get is https://i.sstatic.net/QCViF.png Below are the files in my project: package.json { "name": "angular2-quickstart", "version": "1.0.0", // rest of the package.json file continues... } ...

"Error encountered while executing a code snippet using Navalia in TypeScript

I have been attempting to execute this code snippet from https://github.com/joelgriffith/navalia but despite my efforts, I have not been able to get it running smoothly without encountering errors: navaliatest.ts /// <reference path="typings.d.ts" /&g ...

Protecting Angular Routes: Verifying HTTP-Only Cookie Authentication on the Server- CanActivate Function

I am utilizing an HTTP only cookie for authentication in my server: export const checkCookie = async (req: Request, res: Response) => { const token = req.cookies["token"]; if (!token) { return res.status(401).json({ message: "Unau ...

Opening an external link from the Side Menu in Ionic4: A step-by-step guide

In my Ionic project, I have implemented a side menu in the app.html file that is accessible throughout the entire application. This menu contains items with links that need to be opened externally. However, when trying to open them using InAppBrowser, an e ...

Enhance React components in Deck.GL by including default properties for each child component

I am currently working on a customizable collection of map layers using Deck.GL & React. I have developed a BaseMap component through which I will be passing data layers as react children. This is the current implementation: BaseMap: export const BaseMap ...

Troubles with incorporating Ngb bootstrap into Angular due to dependency conflicts

As a newcomer to Angular, I am venturing into creating an image slider carousel in Angular using the ng-bootstrap component. After setting up my Angular project, I proceeded to install "ng bootstrap" within the project through npm install --save @ng-bootst ...

How to Use a For Each Loop with Google Maps DrawingManager to Create Polygons?

My Ionic 4 Application using Angular 8 incorporates a google maps component where I need to draw and edit multiple polygons, eventually saving their vertices in a database. Hard coding some polygons is easy with getPath() or getPaths(), but I'm utiliz ...

What is the reason behind TypeScript mandating the invocation of super() by the inheriting class, even when the parent class does not have

Just starting out with class-based programming, I've been tinkering with a TypeScript API. Here's the scenario: import { Router } from "express"; export default class BaseController { public router = Router(); } and then I create another cl ...

The compatibility between Typescript methods and event handlers is lacking

Consider this basic TypeScript script class foo { v: number = 1; public bar() { console.log(this.v); } } var a = new foo(); var b = new foo(); document.getElementById('test').addEventListener("click", a.bar); document.getE ...

Showing information from a JSON dataset of users once a specific User ID has been chosen

My task involves displaying user data from an array and then showing the details of the selected user. I attempted to achieve this with the following code: users = USERS; // contains data selectedUser: User; constructor() { } ngOnInit() { } onSelect(i ...

Angular: Determining when a form has returned to its original state

My current task involves working with a reactive form that comes with default values. I need to figure out how to prevent the user from saving changes until they have modified something, and then revert back to the initial state. Although I can subscribe ...

Confirm whether the Iterator type is the same as the AsyncIterator type

Is there a clever JavaScript technique to differentiate between Iterator and AsyncIterator without initiating the iteration process? I'm attempting to create a type checker like this: function isAsyncIterator<T>(i: Iterator<T> | AsyncIter ...

Issues encountered when using AngularJS2's post method to send data to an ASP.NET Core backend result

I recently delved into learning Angular2 and Asp.net core, but I encountered an issue when trying to post an object. Here is the relevant code snippet: Service.ts file: export class SubCategoryService { //private headers: Headers; constructor(private htt ...