When the page is reloaded, the observable in typescript does not update

On my angular page, everything works smoothly when using the button. The page title is correctly displayed. However, upon reloading the page with the reload button, the title remains unchanged.

Here is a snippet of the HTML page:

Snippet from the header section:

 <div class="alternate-theme" color="primary" style="height:30px;">
    <span *ngIf="titleSite$ | async as title" class="center"gt; {{ title }} </span>
 </div>
 <mat-menu #menu="matMenu" [overlapTrigger]="false">
      <button mat-menu-item (click)="navigateMenu('home')">
        <mat-icon color="accent">home</mat-icon>
        <span>Home page</span>
      </button>
  </mat-menu>

a portion of the .ts file :

export class TopMenuComponent implements OnInit {
  public titleSite$: Observable<string>;
}

navigateMenu(tag) {
   this.titleSite$ = this.translate.stream('Sitetitle0'); // Default title
   this.initialTag$ = tag;
   if (tag === "home") {
     this.titleSite$ = this.translate.stream('Sitetitle1');
     this.router.navigate(["/"]);
   }

While clicking the button generates the correct title, reloading the page defaults back to the original title.

Everything functions well except for the scenario when someone triggers a page reload.

How can I compel the page to display the selected title even after a reload?

Appreciate your assistance!

Answer №1

Refreshing the page destroys the variables each time, resulting in the initial value always being the default title.

Potential solutions:

  1. Utilize local/session storage to retain information on the active title, and upon initialization, retrieve the value from there. If it does not exist, revert to the default title.
  2. Save it on a server or an external location for persistent storage.

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

When using PrimeNG's InputOtp component, users are unable to modify the values of input fields

When using the p-inputOtp component from PrimeNG for OTP input, I've observed that changes can be made until three out of four boxes are filled. Once all four boxes are filled, modifying the input is no longer possible. Is there a way to enable change ...

A step-by-step guide on uploading a CSV file in Angular 13 and troubleshooting the error with the application name "my

I am currently learning angular. I've generated a csv file for uploading using the code above, but when I try to display it, the screen remains blank with no content shown. The page is empty and nothing is displaying Could it be that it's not ...

Displaying buttons on each row in an ngx-datatable based on a condition using *ng

In my table, there is a column with a hidden element that appears when a show button is clicked. Currently, clicking the button reveals all hidden fields at once. I want to modify this so that only the input field for the specific row appears when the butt ...

What is the reason for the removal of the `?` decorator in this mapped type? Are there alternative methods to achieve a similar outcome without eliminating it

Challenge In the process of creating a mapped type that excludes properties of type Function, we encountered an issue. Our current method not only eliminates functions but also strips away the optional decorator (?) from the mapped properties. Scenario ...

Encountering a type error while dispatching actions in a React component using TypeScript

When I submit the form, I encounter a type error dispatch(createNote(data)) in handleSubmit. However, if I dismiss the error and proceed with submitting the data, it gets saved successfully in indexeddb. Error: **Argument of type '(dispatch: Dispatch ...

Is the communication between Angular service and component failing when using ngFor?

I am currently using Angular to create a video game page. When a specific title is clicked, the id is sent to make an API call with that specific id. I am able to retrieve the data in the selected-games component where I intend to use it, but when I use ng ...

Dynamically passing output placeholders through ng-content in an Angular component template

Can a component handle dynamic projection in this way? <my-component [date]="2022-03-22"> <p>This particular date falls on a <strong #dayName></strong>! It is in week number <span #weekNum></span> of year &l ...

AngularJS 4 issue: the function enableProdMode has not been found

When I started learning Angular2 using https://angular.io/guide/quickstart, I encountered a console log message saying "Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode." To address this, I added the followi ...

Issues encountered while attempting to utilize the delete function within a CRUD process involving Angular 8, Laravel, and JWT authentication

Currently, I am developing a Single Page Application using Angular 8 on the frontend and Laravel on the backend. The frontend consists of a form with options for users to either edit or delete a user. When the delete button is clicked, I capture the curren ...

Are there any common issues with sorting Nested Tables in Angular?

When I have only one datatable, I am able to sort all values correctly. However, there are times when I have nested tables. For example, users can select a city from the table, then locations within that city are shown and users can choose one of those loc ...

Parent component not receiving value from NG_VALUE_ACCESSOR for radio button selections

Within the parent component, I have developed a form that is intended to function with 3 sets of radio buttons. My approach involved using NG_VALUE_ACCESSOR for communication between the parent and child components. While the data transfer from parent to c ...

Providing data to Angular components

async loadBarchart() { let data = await this.http.get<any>(environment.endpoint + '/api/dashboard/corrosionstudy/' + this.idToUse).toPromise(); //ensure that the displayed data corresponds to the last plantItem clicked on i ...

What exactly is the function of the NextPage feature in Next.js?

Recently, I began incorporating TypeScript into my Next project. Could someone clarify the purpose of the following code snippets for me? import { NextPage } from 'next'; export const Page: NextPage = () => {} After reviewing the documentation ...

Error message: "Duplicate identifier '_' found in Lodash index.d.ts:244:12 during Angular2 web pack build process."

UPDATE: Please read below I've scoured numerous articles regarding the issue of duplicate identifiers but have yet to come across a solution that can resolve this particular problem. Here's the contents of my package.json: "dependencies": { ...

The challenge of injecting services into Jest test cases using Angular 17 Functional Resolver

Overview Greetings, I am currently in the process of updating my Angular application from version 14 to version 17. One of the tasks involved in this update is replacing the deprecated Resolver classes with the new ResolverFn implementation. The challen ...

Looking to receive detailed compiler error messages along with full imports in Visual Studio Code for TypeScript?

Currently, I am trying to edit a piece of typescript code in Visual Studio Code. However, I encountered a compiler error message that looks like this: The error states: Type 'import(\"c:/path/to/project/node_modules/@com.m...' is not assign ...

Is the module eagerly loaded or is it imported/exported within the core module?

To enhance the cleanliness of my AppModule, I decided to import a feature module that is eagerly loaded in the CoreModule. This CoreModule is then imported only once in the AppModule. I have noticed something intriguing - whether I export or import the fe ...

Angular: Display Component Only, Concealing Header and Footer

Is there a way to display only the Angular Component on a page while hiding everything else, including the header and footer that are automatically added to all components without coding for them in HTML? Check out the Component Picture in a Green Border ...

Using TypeScript to define task invocation parameters with AWS CDK's CfnMaintenanceWindowTask

Currently, I am utilizing AWS CDK along with the library @aws-cdk/aws-ssm and TypeScript to construct CfnMaintenanceWindowTask. The code example I am working on is derived from AWS CloudFormation documentation, specifically for "Create a Run Command t ...

I am wondering why the content is not showing up when using state.map(foo => <>{foo}</>) in my code

Why does the first tsx code display the state.map properly while the second code displays nothing? Despite both pieces of code performing the same task in the same way, one list is correctly displayed while the other state.map has never rendered anything, ...