The issue with subscribing to a subject in Angular 8 appears to be causing a problem. It looks like the subject is not functioning properly

I am encountering an issue where the subject I am trying to emit while fetching data is not triggering any events.

Even in the component's ngOnInit(), I am unable to see the log written inside the subscription code.

The service code snippet looks like this:

@Injectable()
export class RecipeService {

  recipesChanged = new Subject<Recipe[]>();

setRecipes(recipe: Recipe[]){
    console.log('setrecipes() is called');
    this.recipes=recipe;
    console.log('Recipe list after setRecipes(): ' + recipe);
    this.recipesChanged.next(this.recipes.slice());
}

Moving on to the component:

export class RecipeListComponent implements OnInit, OnDestroy {
  recipes: Recipe[] = [];
  subscription: Subscription;

  constructor(private recipeService: RecipeService, 
    private router: Router, private route: ActivatedRoute) {}

  ngOnInit() {
    console.log('ngOnInit is called of recipe-list')
    this.subscription = this.recipeService.recipesChanged
      .subscribe(
        (recipes: Recipe[]) => {
          this.recipes = recipes;
          console.log('Recipe-list recipes are: ' + recipes);
        }
      );
}

Answer №2

My issue stemmed from an excessive number of RecipeService provider declarations, resulting in multiple active instances running simultaneously. DS

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

Why does my Angular CLI default to generating CSS files instead of SCSS?

Before transitioning to Angular 9, my project was set up to use SCSS when running the ng generate command, and it worked perfectly. To my disappointment, when I created my first component using ng g c in Angular 9, it generated a css file instead of a scs ...

Formatting Angular Pipes for Large Numbers

I'm currently handling large figures that I am formatting in the following manner: {{total | number:'1.0-0'}} However, the result is 45,986,592. My desired format is to display only the millions and then have one decimal place, like 45.9 ...

Is it possible for one component to access the viewmodel of another component that is not related?

I am working with Typescript, React, and Material-UI. I'm using plain React without Redux or Flux. My goal is to scroll to the TabsComponent and select a specific tab when a button in the ButtonComponent is clicked. These components are not directly ...

Customizing styles for specific days on the mat-calendar component

Does anyone have suggestions on how to add a custom class to specific dates in an array? I want to highlight certain events. Here is the HTML code: <mat-card-content> <div class="date-picker"> <mat-calendar [selected]="selectedDates" ( ...

Develop a TypeScript Module that consolidates all exports

My Goal with TypeScript Modules I aim to streamline my TypeScript project by creating a module that contains all the necessary class exports. Currently, I find myself using relative import statements in my classes, which can make maintenance challenging i ...

What is the most efficient way to execute useEffect when only one specific dependency changes among multiple dependencies?

My main objective is to update a state array only when a specific state (loadingStatus) undergoes a change. Yet, if I include solely loadingStatus as a dependency, React throws an error requesting all dependencies [loadingStatus, message, messageArray, set ...

Bootstrap 4 - switch button dynamically updates icons from ">" to "<" depending on the current state

Is there a way to display an icon on the right side of a Bootstrap 4 button, such as ">" or "<", depending on whether the button is active or not? This is for an Angular-based application. The default appearance of a Bootstrap toggle button is: < ...

Mistakes & Failures: Problems encountered while running tests on karma

Whenever I try to execute mvn install in an Angular project, I encounter errors and build failures during the execution of unit tests. The error messages indicate issues with the '@angular-devkit/build-angular/plugins/karma' karma plugin: [INFO] ...

Showing a base64 image in Angular 2

I have a challenge where I am attempting to display an image from a server using a base64 string. The specifics of the http and server are not crucial for my inquiry (in essence, it is functioning). However, the current code I have is not displaying the im ...

Exploring Firestore Snapshots - Keeping an Ear Out for Fresh Documents

Currently, I am in the process of developing a dynamic scrolling pagination feature using Firestore. My approach involves loading a limit of 10 real-time documents into an array that will be displayed on the screen. The plan is to implement the loadMore() ...

The CORS policy specified in next.config.js does not appear to be taking effect for the API request

I am currently working on a Next.js application with the following structure: . ├── next.config.js └── src / └── app/ ├── page.tsx └── getYoutubeTranscript/ └── getYoutubeTranscript.tsx T ...

Illuminating JavaScript code with the help of ngx-prism

Looking to enhance the readability of code on my website, I decided to incorporate syntax highlighting. After some research, I came across ngx-prism. Following the steps outlined in its documentation: I executed the following commands: npm i --save @n ...

Peeling off the layers of an array declared as const to reveal its mutable version without being restricted to tuples

I'm facing a challenge with an array declared as as const: // example of a simple mock class class Child { _ = "" } const child = new Child(); const schema = [[child], child] as const; // readonly [readonly [Child], Child]; This array rep ...

Dynamically Disabling Form Control in Angular 2

There is a basic form with two form controls that initially have required validation. In addition to the form controls, there is a button in the template. When this button is clicked, it triggers a change in the behavior of the form group. Both form contr ...

When using a ngForm, submitting the form is triggered each time an action button is clicked

I have a form with multiple buttons in my ngForm. Each button has a different action, such as changing the status used for *ngIf condition check. However, every time I click the Create or Upload button, the form is submitted again and a new record is creat ...

What could be causing the issue with my React Native app's release version not opening on a physical Android device?

I encountered an issue while trying to install the Release version of my application. In order to test both the debug and release versions on my physical and virtual devices, I utilized the following commands: ./gradlew assembleDebug, ./gradlew assembleRel ...

Guidelines for segregating a Union from an Array

I'm currently utilizing graphql-code-generator to automatically generate TypeScript definitions from my GraphQL queries. I have a specific union within an array that I am trying to extract in TypeScript. Is this feasible? Although I came across an exa ...

Issue with accessing custom method in subclass in Typescript

Recently delving into TypeScript, I decided to subclass Error and add a new method called getCode() in my MyError class. export class MyError extends Error { public code: number; constructor(code: number, message?: string) { super(mes ...

Both an Angular and Java application seamlessly operating within a single domain

Recently Updated: Project 1: PHP, HTML, CSS, Java, mySQL Project 2: Angular, Spring Boot, PostgreSQL I am currently working on breaking off a part of Project 1 into a separate project (Project 2) that will operate independently with its own database. ...

Exploring the process of dynamically updating a form based on user-selected options

I need assistance with loading an array of saved templates to be used as options in an ion-select. When an option is chosen, the form should automatically update based on the selected template. Below is the structure of my templates: export interface ...