Is there a way to get interpolation working outside of onInit?

In one component, I have set up a functionality to subscribe to an HTTP GET request from a service and store the response in a variable. The service contains a Subject as an observable so that it can be subscribed to in another component. However, while I am able to successfully retrieve and log the object's correct value in the new component, I encounter an issue where I am unable to display it in HTML using interpolation.

Here is the service setup:

private transactionsToSend = new Subject<any>();
currentTransactions = this.transactionsToSend.asObservable();

transactions() {
    return this.http.get<any>(`${this._url}/transactions`)
}

sendTransactions(transaction: Transactions){
    this.transactionsToSend.next(transaction)
}

Subscription and transmission in the receiving component:

this.transactionsService.transactions().subscribe(
     (res: Transactions) => {
       this.transactions = res;

       this.transactionsService.sendTransactions(this.transactions);
     })    

The process of retrieving data in another component:

getReceives() {   
    this.transactionService.currentTransactions.subscribe(     
      (res: any) => {
        this.valueReceita = 0;
        this.currentTransactions = res.transactions;
        for (let x = 0; x < res.transactions.length; x++) {
          if (this.currentTransactions[x].chart == "+") {
            this.valueReceita += parseFloat(this.currentTransactions[x].value)
          }         
      }
      console.log(this.valueReceita) // This value is 99999
    })
}

However, when attempting to display the received data:

<p>{{valueReceita | currency:'BRL'}}</p>

The value does not appear until another request is made.

Answer №1

Substitute the topic with BehaviourSubject

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

Concealing Dropdown Box Information Based on a Customized List in Angular

Is there a way to remove an item from a dropdown list once it has been selected? For example, if I have a dropdown box with options 'Dog', 'Lion', and 'Cat', how can I make it so that once I select 'Dog', it will no ...

In TypeScript, what specific term denotes a type of data?

Given the following code snippet: class Foo { } interface TypeProvider() { type(): ?; } class Bar implements TypeProvider { type(): ? { return (Foo); } } class Baz implements TypeProvider { type(): ? { return (Bar); ...

Node.js allows for keeping pipe and sockets open even after streaming an HTTP response

My current challenge involves streaming data from an HTTP response to a cloud storage provider within an internal service. const response = await request<Readable>({ headers: httpOpts?.headers, data: httpOpts?.data, url, method, responseTyp ...

Tips for composing content on a sanitized input?

In my small application, I have a feature where a question is displayed with certain words hidden and needs to be filled in by the user. The format of the question looks like this: The {0} {1} {2} his {3} off To achieve this functionality, I wrote the f ...

The transition from Angular 8 universal to the 9 Universal Version encountered a setback: the function require.resolve is not recognized

https://i.sstatic.net/IeLyV.png After delving into angular-errors, I stumbled upon the following error: [error] TypeError: require.resolve is not a function at SchematicImpl.<anonymous> (E:\B2C-NG-9\b2c\node_modules\@ngu ...

What is the best way to bring in a SCSS file within another SCSS file?

When working with SCSS, I find myself using a lot of variables. To keep things organized, I create a separate file that contains all of my SCSS variables. The next step is to import this file into other SCSS files. Here's what I tried: Create a fil ...

What could be the reason my RxJS Observable chain does not run again when new emissions are made?

Currently, I am facing a unique challenge while working with RxJS in an Angular service. The issue revolves around two observable chains designed to enhance a stream of notifications with user data. One chain functions correctly, allowing for multiple trig ...

Oops! The program encountered an issue on the production environment, but it's running smoothly

When I execute Webpack using the command node node_modules/webpack/bin/webpack. js --env. prod An error message is displayed below. However, when running in --env. dev mode, the command executes without any issues. Can't resolve './../$$_gen ...

The state array is rejecting the value from the other array, resulting in null data being returned

I am currently attempting to extract image URLs from an HTML file input in order to send them to the backend and upload them to Cloudinary. However, I am facing an issue where despite having the imagesArr populated with images, setting the images state is ...

Angular 2 testing error: Unable to connect to 'ngModel' as it is not recognized as a valid property of 'input'

Currently, I am experimenting with angular2 two-way binding for the control input. Below is the issue that I encountered: An error occurred: Can't bind to 'ngModel' since it isn't a known property of 'input'. Contents of app. ...

Having trouble with i18n types not functioning correctly in Typescript and Nuxt?

I am currently utilizing NuxtJS/i18n with TypeScript and have included its types in my TS config: { "compilerOptions": { "types": [ "@nuxt/types", "@nuxtjs/i18n", ] } } However, when attempti ...

Ways to encourage children to adopt a specific trait

Let's discuss a scenario where I have a React functional component like this: const Test = (props: { children: React.ReactElement<{ slot: "content" }> }) => { return <></> } When a child is passed without a sl ...

Altering the background color of an individual mat-card component in an Angular application

Here is the representation of my mat-card list in my Angular application: <div [ngClass]="verticalResultsBarStyle"> <div class="innerDiv"> <mat-card [ngClass]="barStyle" *ngFor="let card of obs | async | paginate: { id: 'paginato ...

Ways to modify the information retrieved from JSON?

I've integrated the OMDB api (Online Movie DB) into my project. The interface I created is returning the expected data types, with data being returned as {Search: Array(10), totalResults: "31", Response: "True"} when using the http get method. The spe ...

Issue with Angular: Mobile view toggle button in the navbar is unresponsive

While the dropdowns are functioning correctly in web mode, the navbar toggle isn't working as expected in small screens or mobile mode. I've been trying to figure out the issue by referring to code from CodePen that I am learning from. Despite i ...

Stop material button from animating when clicked

On my webpage, I have created a unique button structure using Angular Material design: <button mat-button class="library-tile-button"> <button mat-button class="edit-library-button"> <img class="edit-library-img" src="..."/> ...

Uniting 2 streams to create a single observable

I am in the process of merging 2 different Observables. The first Observable contains a ShoppingCart class, while the second one holds a list of ShoppingItems. My goal is to map the Observable with shopping cart items (Observable<ShoppingItems) to the i ...

Having trouble achieving success with browser.addcommand() in webdriverIO using typescript

I tried implementing custom commands in TypeScript based on the WebdriverIO documentation, but unfortunately, I wasn't able to get it working. my ts.confg { "compilerOptions": { "baseUrl": ".", "paths": { "*": [ "./*" ], ...

Injecting constructor for Angular standalone component and service

I am encountering an issue with injecting a service through the constructor of a standalone component. The service is declared as Injectable with the provider set to root: @Injectable({ providedIn: 'root' }) export class ProductsService {...} ...

Can a single shield protect every part of an Angular application?

I have configured my application in a way where most components are protected, but the main page "/" is still accessible to users. I am looking for a solution that would automatically redirect unauthenticated users to "/login" without having to make every ...