Having trouble with my Angular subscription - not behaving as I anticipated

I am facing an issue on my shop page where I have a FilterBarComponent as a child component. On initialization, I want it to emit all the categories so that all products are rendered by default. However, on my HomePageComponent, there is a button that allows users to navigate to the shopPage and view a specific category, for example, a "view shirts" button. The problem arises because the default categories array update occurs after the subscriber function finishes, and the event emitter does not fire in the subscriber. This relates to another question of mine which you can find here: Angular EventEmitter is not emitting in subscriber

FilterBarComponent


categories = [];
@Output() filteredCategory = new EventEmitter<any>();
@Output() maxiumPriceEmitter = new EventEmitter<any>();
categorySub: Subscription;

formatLabel(value: number) {
    return 'R' + value;
}

constructor(private shopService: ShopService) {}

ngOnInit() {
    this.initCategories();
    this.filterCategories();
    this.updateCategories();
}

filterCategories() {
    this.shopService.filterCategories.subscribe(
        (fCategory: string) => {
            this.categories.map(category => {
                category.checked = category.name === fCategory;
            });
            this.updateCategories();
        }
    );
}

initCategories() {
    this.categories = [
        { name: 'dress', checked: true, displayName: 'Dresses' },
        { name: 'top', checked: true, displayName: 'Shirts' },
        { name: 'skirt', checked: true, displayName: 'Skirts/Pants' },
        { name: 'purse', checked: true, displayName: 'Purse' },
        { name: 'bag', checked: true, displayName: 'Bags' },
    ];
}

updateCategories() {
    const categories = this.categories
      .filter((category) => {
        return category.checked;
      });
    console.log(categories);
    this.filteredCategory.emit(categories);
}

In the console, initially I get the correct result, but then the categories array resets.

[{}] 
{name: "top", checked: true, displayName: "Shirts"}

[{…}, {…}, {…}, {…}, {…}] 
{name: "dress", checked: true, displayName: "Dresses"}
1: {name: "top", checked: true, displayName: "Shirts"}
2: {name: "skirt", checked: true, displayName: "Skirts/Pants"}
3: {name: "purse", checked: true, displayName: "Purse"}
4: {name: "bag", checked: true, displayName: "Bags"}
length: 5 

The Observable in ShopService

filterCategories = new BehaviorSubject("category");

Answer №1

I want to give credit to @Józef Podlecki for his helpful response to another one of my questions.

After considering his advice, I've realized that using a BehaviorSubject instead of a regular subject in the ShopService will be more beneficial.

filterCategories = new BehaviorSubject("all");

Incorporating this change into the Filter Bar Component:


  ngOnInit() {
    this.initCategories();
    this.filterCategories();
  }

  filterCategories() {
    this.shopService.filterCategories.subscribe((fCategory: string) => {
      if (fCategory === 'all') {
        this.updateCategories();
      } else {
        this.categories.map((category) => {
          category.checked = category.name === fCategory;
        });
        this.updateCategories();
      }
    });
  }

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

I'm having trouble sending my token to the header because my app is not storing it correctly

Upon implementing validaToken() at login, I encounter the following error even though the token is correctly stored in local storage. I have tried putting it on the server side, but unfortunately, it does not seem to work: server.app.use( bodyParser.urle ...

Unexpected response received from ajax call when supplying data to flot.js

Within this script block, I am retrieving data from a database in order to create a line graph. However, the data is returning as undefined and the graph is not being generated. var d; var arr = []; $(function() { var data; $.ajax({ da ...

Remove the JSON object by comparing IDs between two JSON objects in JavaScript or Node.js, deleting it if the ID is not found

let data = fetchData(); let anotherData = getAnotherData(); let result = data.reduce((accumulator, current) => { if (!accumulator[current.system.name]) { accumulator[current.system.name] = {}; } let detailsObject = {}; Object.keys(current. ...

The Vue component mistakenly saves the image to the incorrect location when the file @change event is triggered

I've encountered an issue with my image slider component. Users have the option to add an extra image to the end of the slider, but when there are multiple instances of the same slider component on a page, it always adds the image to the first compone ...

City-based Address Suggestions with Google API Places

I have been struggling to find a solution to this specific issue without any luck. Your assistance would be greatly appreciated. Currently, I am attempting to implement autocomplete address functionality using Google API with the following code: var inpu ...

Angular 6 - The requested resource does not have the necessary 'Access-Control-Allow-Origin' header

I am currently working on an Angular 6 project that includes a service pointing to server.js. Angular is running on port: 4200 and Server.js is running on port: 3000. However, when I try to access the service at http://localhost:3000/api/posts (the locat ...

In what way can I decipher a section of the URL query string within my AngularJS application?

Here is a snippet of code that I am working with: var search = $location.search(); if (angular.isDefined(search.load) && search.load != null) { if (search.load = "confirmEmail") authService.confirmEmailUserId = search.userI ...

The mouse scurries away once the div height has been adjusted

How can I make the height of #header change when hovering over #hoverme, and then revert back to its original height when the mouse leaves #hoverme? If anyone knows a solution, please check out my jsfiddle as it's not working as I intended. Here is ...

After refreshing the page, the JWT token is no longer available

I've recently launched my app on Heroku at this link: Give it a try yourself to witness the issue. Everything seems to be working fine until you refresh the website. The JWT token is stored in LocalStorage but gets lost from the header upon page re ...

"Can you tell me the method for obtaining an array within an Angular

Within the realm of PHP, there exist certain elements within an array: $this->data['messages']['ms'][] = 'Line1'; $this->data['messages']['ms'][] = 'Line2'; along with a method that return ...

When trying to access the page via file://, the cookies are not functioning properly

My HTML code is functioning properly in Firefox and even on the W3Schools website when tested using their editor in Chrome. However, when I run my code in Chrome from Notepad++, it doesn't seem to work. It appears that the body onload event is not tri ...

What are the optimal strategies for managing various components within an Angular (2) Web application?

I am seeking advice on Angular 2+ Webapps and have a few questions. What is the recommended approach for managing a publicly available info page, an authentication page, and a protected page for signed-in users? Should I consider separate Angular Apps ...

Tips for modifying an axios instance during response interception

Is there a way to automatically update an axios instance with the latest token received in a response, without making a second request? The new token can be included in any response after any request, and I want to make sure that the last received token ...

Tips for resolving an issue with an overflowing Uber React-Vis multicolor bar chart

I am trying to create a multi-colored bar chart using Uber's react-vis library. However, I am encountering an issue where the leftmost bar of the chart is overflowing below the YAxis instead of remaining contained to the right of it. You can view the ...

Input does not have access to ngModel's property

I'm facing an issue with NgModel as it doesn't seem to work when I try to save data from an input field. Uncaught Error: Template parse errors: Can't bind to 'NgModel' since it isn't a known property of 'input'. (" ...

Utilizing an array for assigning values in conditional statements

I have been experimenting with toggling a CSS style on a couple of elements using an if statement. Currently, I have it working with several if statements, but I believe it can be simplified by utilizing an array with the values and just one if statement. ...

JavaScript Application - Problem with Universal Windows Script

I recently built a website utilizing material design lite for the aesthetics: Here are the scripts included: <script src="./mdl/material.min.js"></script> <script src="Scripts/angular.min.js"></script> The necessary .css fi ...

Is there a simpler way to retrieve data from PHP or to efficiently filter the data once it's been retrieved?

Creating a business directory website involves fetching data from a database. The issue arose when attempting to apply a function uniformly to all boxes, as only the first one with the specified id would function correctly. To address this problem, the fol ...

Adjust ion-select label width across the entire screen in Ionic version 6

I recently began working on a fresh project using Ionic v6. As part of the development, I included a basic ion-select element in my HTML code: <ion-item> <ion-select placeholder="Select Option"> <ion-select-opti ...

Next.js directs API requests to the root URL

I'm currently working with an API handler pages/api/[slug]/[uid].ts My goal is to redirect the requests to the main root of my application, specifically: http://localhost:3000/[slug]/[uid] What steps do I need to take in next.config in order to mak ...