The issue of binding subjects in an Angular share service

I established a shared service with the following Subject:

  totalCostSource$ = new Subject<number>();

  shareCost(cost: number ) {
    this.totalCostSource$.next(cost);
  }

Within my component, I have the following code:

  private incomeTax: number = 18;
  income: number;

  constructor(private costService: CostService) { }

  ngOnInit() {
    this.costSubsription = this.costService.totalCostSource$.subscribe( cost => {
      this.income = cost * this.incomeTax / 100;
      console.log(this.income); // displays correct value in console
    });
  }

However, I am facing an issue with updating the income variable in my view. Even though the correct value is displayed in the console, the view does not refresh. How can I resolve this problem?

Answer №1

@johndoe32 I can't provide a definitive reason without seeing the template or component that invokes shareCost.

Here are some troubleshooting steps:

  • Ensure that shareCost is being called after totalCostSource$.subscribe
  • If you are using onPush or have detached change detection, consider manually detecting changes
  • Verify that subscribe is returning a valid value of type number

If these steps don't resolve the issue, please provide:

  • The HTML Template where income is referenced
  • The Typescript code for both components (the one calling shareCost and the one subscribing to subject)

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

The 'BaseResponse<IavailableParameters[]>' type does not contain the properties 'length', 'pop', etc, which are expected to be present in the 'IavailableParameters[]' type

After making a get call to my API and receiving a list of objects, I save that data to a property in my DataService for use across components. Here is the code snippet from my component that calls the service: getAvailableParameters() { this.verifi ...

What is the process of playing blob videos (avi, mov) in Angular14?

I've been struggling with this issue for quite some time without knowing how to resolve it. After some research, I came across a similar question: how to play blob video in angular. However, the problem is that the demo provided in the answer does no ...

Tips for deactivating the matMenuTrigger when the mat menu does not display any menu items

How can I dynamically disable the mat menu trigger when no items are rendered due to ngIf conditions in the mat-menu? <button mat-button [matMenuTriggerFor]="menu" [disabled]="disableTrigger" > Trigger </button> <mat-menu #menu ...

Combining Multiple Arrays into a Single Array

Is there a way to combine this merge operation that creates one array using forEach into a single array at the end? affProd.pipe(mergeMap( event1 => { return fireProd.pipe( map(event2 => { const fi ...

Developing a common module for use across deferred loading paths

I have been working on developing an Angular 11 application and am currently in the process of creating a SharedModule. My goal is to prevent loading common modules repeatedly across my lazy loaded routes. To achieve this, I created a shared module and imp ...

The preflight request in Angular2 is being rejected due to failing the access control check: The requested resource does not have the 'Access-Control-Allow-Origin' header

I encountered an issue while attempting to execute a basic POST request to establish an account using an API in .NET. The process fails with the mentioned warning title. Interestingly, performing the same request in Postman (an API testing tool) yields a s ...

Next.js: Importing from a new endpoint triggers the code execution once more

Here is a simplified example I created for this specific question. Imagine I want to maintain a server-side state. components/dummy.ts console.log('initialize array') let number: number = 0 const incrementValue: () => number = () => numbe ...

Issue with Angular 6 Material2 mat-table MatRipple causing errors

When I try to use MatTable with a data source in Angular 6 and add sorting or pagination, I encounter the following error: ERROR Error: Uncaught (in promise): Error: Can't resolve all parameters for MatRipple: ([object Object], [object Object], [ob ...

The introduction of an underscore alters the accessibility of a variable

When working in Angular, I encountered a scenario where I have two files. In the first file, I declared: private _test: BehaviorSubject<any> = new BehaviorSubject({}); And in the second file, I have the following code: test$: Observable<Object& ...

Angular 2 - Changes in component properties not reflected in view

I'm currently delving into Angular 2 and as far as I know, interpolated items in the view are supposed to automatically update when their corresponding variable changes in the model. However, in the following code snippet, I'm not observing this ...

Passing input values from a parent component to a child component in Angular is not functioning as expected

I am facing an issue with implementing a basic @Input in Angular. Despite setting the value in the parent component template as follows: <app-summary-data-row> [test] = "'ttt'" </app-summary-data-row> In the child component, I h ...

Converting POST data into an array using Angular

I am facing an issue with my post request setup in a NodeJS app. The data sent returns as an array, but I am struggling to convert it into the desired format. My Angular code is structured as follows: newWord = ''; keyword = ''; onCli ...

transforming an angularJS directive into an angular6 component/directive

I have inherited an angularJS directive from an old application that I need to convert to angular6. Since I am not familiar with angularJS and only work with angular6, I am seeking guidance on how to proceed. Below is the existing angularJS code: define ...

Angular 2 can efficiently generate multiple HTTP requests simultaneously from a single HTTP request

Backend API's: url: [ { "name": "ABC", "occupation": "Student", "address_url": "http://www.sample.com/address/person=hgjgjgyyfhg" }, { "name": "ABC1", "occupation": "Carpenter", "address ...

Having trouble using the Aceternity interface as it keeps giving me a type error

I am facing an issue when trying to integrate the Acternity UI component library with nextjs. The error message I keep encountering is: "Property 'pathLengths' is missing in type '{}' but required in type '{ pathLengths: MotionValu ...

Angular 5: There was an issue with the property not defined for lowercase conversion

I keep encountering this error whenever I attempt to filter a column of a table. The data is retrieved from a FireStore cloud collection and the 'auteur' field is defined in each document. Here is how my component looks: import { Component, OnI ...

Issue with Progressive Web App functionality when using Angular Pre Rendering

Currently, I am working on an Angular 10 Universal project. Whenever I execute the following command: "build:ssr": "ng build --prod && ng run PROJECT:server:production && node dist/PROJECT/server/main.js", I can see th ...

Unable to bring in a personalized npm package using its package title

Currently, I am loosely following a tutorial on creating an angular npm package named customlib. This package is intended for managing dependencies across my projects without the need to make them public on npm. The tutorial can be found here. However, I ...

Maximizing Azure Web App capabilities to host multiple applications

I have developed an app using ASP .NET Core MVC + Angular and now I need to deploy it for three separate customers. Each customer currently has their own database. Is it feasible to create multiple sites within a single Azure web app (such as mysite.com/ ...

Develop a Typescript interface containing all the necessary properties for a specific JSX component

My goal is to create a ControlledInputComponent that consists of a label and an input inside a div. I aim to make it highly customizable as I plan on using it in various ways. At the moment, I have included all the necessary elements, but I would like to e ...