Storing information upon refresh in Angular 8

When it comes to inter-component communication in my Angular project, I am utilizing BehaviourSubject from RXJS. Currently, I have a setup with 3 components:

  • Inquiry Form

    Where users enter an ID number to check for summon-related information. This data is then sent to the Inquiry Response component.

  • Inquiry Response

    This component receives summon details from the Inquiry Form and displays them as checkboxes. Users can select summons (total amount) which are then sent to the Payment Component.

  • Payment Component

    Here, the total summon amount from the Inquiry Response component is displayed.

The issue arises when a user reloads the page on the Payment Component. The data retrieved from the Behaviour Subject appears empty. While I looked into using localStorage and other state management libraries like ngrx, they don't seem feasible for my current project stage. Are there any best practices or alternative solutions to tackle this problem effectively?

I have attempted the following approach:

payment-store.service.ts

  private transactions = new BehaviorSubject<any>({});
  public transactions$ = this.transactions.asObservable();

    setTransaction(data) {
    this.transactions.next(data);
  }

payment-component.ts

  ngOnInit() {
    this.paymentStore.transactions$.subscribe(
      response => {
        this.totalSummon = response;
      }
    );
  }

For a more detailed look at the code, you can check out the StackBlitz demo. Any suggestions or solutions to resolve this challenge would be greatly appreciated.

Answer №1

In cases where storing data locally is not feasible, the most straightforward solution would be to utilize a database for storage. It is advisable to opt for a database that does not necessitate the creation of your own backend.

An excellent choice for swift implementation would be Firebase, which only requires initial setup and the establishment of a few security rules to regulate user access to the data.

If you are unfamiliar with Firebase, I recommend exploring @angular/fire, specifically designed for Angular implementations. Detailed documentation can be found at:

https://github.com/angular/angularfire2

This resource also includes example documents to facilitate your introduction to the platform.

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

Dynamic form validation using jQuery

I am facing a challenge with validating a dynamic form on the user side. My goal is to ensure that if a user fills out one column in a row, they are required to fill out the remaining columns as well. For example, filling out the CC # should prompt the use ...

Using React and TypeScript to conditionally set props in a component

I am trying to assign a value to the component's prop when a variable is defined. Below you can find my current code. import Cropper from 'react-easy-crop' ... interface State { ... coverFile: File | null; ... } class Test extends React ...

Retrieve the id of the anchor tag that was selected upon clicking, and then dynamically change the content of another div

Seeking guidance on how to dynamically change the content of a div element based on which anchor tag is clicked. Below is the HTML structure and JavaScript function I have attempted: HTML: <div> <a id="a1" href="javascript: changeDiv();">tag1 ...

Continuously encountering CORS errors despite activating them, using .NET Core in conjunction with an Angular client

I recently encountered a CORS issue in my .NET Core 3.0 Web API project despite having enabled CORS in the startup file. Here's how I configured it: ConfigureService services.AddCors(options => { options.AddPolicy("AllowOrigin", builder => b ...

Creating experiences for websites controlled by gestures

It's been a great experience working on a website that utilizes gesture-based technology. My inspiration for this project came from various sources, including this link. Despite researching extensively through websites, Google, Wikipedia, and GitHub, ...

Increase the current time by 50 minutes

I have a dropdown menu with various time options like this: <select name="" id="delyvery-hour"> <option value="18:00">18:00</option> <option value="18:05">18:05</option> <option ...

JavaScript code to activate a text box when a checkbox is clicked

I have a dynamic table that is generated by JavaScript based on the selected item from a dropdown list. The table consists of a column for checkboxes, a column for displaying names, and a column for inputting quantities. I would like to have all textboxes ...

Access a document and analyze its information

I am currently working with a file upload control that stores the selected file within it, as shown in the code snippet below: <div class="Block"> <label id="lbl">File </label> <input #fileInput type='file'/> </div ...

JS Issue with Generating Content

Introduction( kind of :) ) Starting with the process of generating 5 coffee beans for each cup of coffee. The coffee class includes a strength attribute, and my goal is to visually distinguish the coffee beans which have a strength greater than the select ...

Implementing a design aesthetic to installed React components

After downloading a react multiselect component created by an individual, I installed it using npm install .... and incorporated the component in my react code with <MultiSelect .../>. The dropdown checkbox 'menu' is designed to allow users ...

What could be causing the NoScript tag to malfunction across different web browsers?

I've incorporated the NoScript tag into my JSP pages in the head section. To avoid conflicts with tiles, I made sure not to include multiple NoScript tags. However, I am experiencing issues in Internet Explorer where it doesn't seem to be working ...

CSS that relies on the presence of a specific class

I'm a CSS novice and I have encountered a scenario where I need to apply a CSS class only when another class is not present. <div class="border-solid p-2 bg-white mt-1 h-fit" [ngClass]="SOME_CONDITION ? 'tile-con ...

Using Rollup alongside @rollup/plugin-babel and Typescript: Anticipated a comma, received a colon instead

I've encountered a problem while working with Rollup 4: [!] RollupError: Expected ',', got ':' (Note that you need plugins to import files that are not JavaScript) src/index.ts (48:19) Although my Babel configuration appears to ...

Mastering the Art of jQuery: Easily Choosing and Concealing a Div Element

I'm currently facing challenges in removing a div upon successful AJAX completion. The issue I'm encountering is that the word "Added" appears twice after success, indicating that I am not properly selecting the two divs containing it. Any sugges ...

Tips on saving a moved option item from one table to another in HTML:

Having an issue with dragging tables between different lists in Angular. The tables are generated by separate ngFor loops and I am using cdkDrag for the drag and drop functionality. The problem arises when I set an option in a dropdown and then drag the ta ...

The compilation process encountered an error: TypeError - Unable to access property 'exclude' as it is undefined (awesome-typescript-loader)

After successfully converting my existing Angular 2 project into Angular 4, I encountered the following error: Module build failed: TypeError: Cannot read property 'exclude' of undefined For more details, please refer to the attached image bel ...

Exploring real-time data updates using React and the setInterval method

Every time my component loads, I am attempting to continuously poll the Spotify API using setInterval. However, during testing, an error message pops up: Invalid Hook Call..etc. I suspect that the issue stems from using useEffect within another useEffect w ...

Error in the execution of the if statement within $(window).width() when it is not supposed to be

Hello everyone, I'm facing an issue with the jQuery $(window).width when using if statements. It seems that my function is still running even when the window width is smaller than 991 pixels, although my if statement specifies that it should run only ...

What is the process for generating an HTML document from start to finish with the 'html-element' node module?

Here is an example that demonstrates a flawed method: const HTML = require('html-element'); const doc = `<body> </body>`; const page = HTML.document.createElement(doc) page.appendChild('<div>1</div>') page.append ...

Grouping emitted events using RxJS in a Node.js environment

Currently, I am interfacing with a database and receiving the results in a continuous stream of events labeled 'db_row_received'. My aim is to categorize these results based on the company Id, however, I am encountering an issue where the subscri ...