Modify the buttons in the Angular Material Nav-bar according to the current page

I've utilized Angular Material to design my navbar in the app.component.html page. Initially, it features a LOGIN button which should be hidden once the user successfully logs in.

Current method: I'm currently disabling the login button based on a boolean attribute in local storage. However, the button remains enabled until I refresh the page.

Since my Navbar is a shared component, is there a way to dynamically add a new button without requiring a page refresh when the data is updated?

app.component.html:

<app-navbar></app-navbar>

  <router-outlet></router-outlet>

Navbar.component.html:

<mat-toolbar color="primary" class="mat-elevation-z">
  <span><mat-icon>post_add</mat-icon> ED-Planner</span>
  <div class="spacer">  </div>
    <button mat-button [routerLink]="['/user']">Home</button>
    <button mat-raised-button color="accent" [routerLink]="['/user/signup']">Signup</button>

</mat-toolbar>

Any assistance would be greatly appreciated

Answer №1

Typically, user data is stored in localstorage for easy access. Alternatively, you can create a service to store the data and ensure it's providedIn root for a single instance.

Once a user logs in, you can save their data in the service or localstorage (remember to prioritize security). Here's an example of how you can handle this:

<ng-container *ngIf=isLoggedIn; else loggedOut>
 //Content displayed when user is logged in, such as profile buttons
</ng-container>
<ng-template #loggedOut>
//Section for user login
</ng-template>

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

Generate an array of checked inputs to be used when posting to a REST API

I have been using .push() to create a checked array of "List" inputs for posting to a REST API. However, it doesn't seem to be working correctly. When unchecking an item, it is not automatically removed from the array. Does anyone have a better solut ...

Angular has a way of causing confusion when it comes to differentiating between anchor scrolling and wildcard routing, denoted by

I am facing an issue in my Angular project where anchor scrolling is conflicting with wildcard routes. Here is what I have done in the project: I have enabled anchor scrolling in my app.module.ts like this: RouterModule.forRoot( [ { path: &ap ...

What are effective methods for testing HTML5 websites?

I'm currently working on a project to create a dynamic website using HTML5. The first page will prompt the user for specific inputs, and the following page will adjust accordingly based on these inputs. My main concern now is how to effectively test ...

Incorporating server-side rendered components into a Vue.js parent component

I have a server-side rendered page that includes information about "orders" and a root Vue object initialized as the "parent object." Is it feasible to assign the rendered HTML orders as children of that parent Vue object? Vue.js is known for its dynamic ...

Angular JS Integration with PapaParse

Currently enjoying the efficiency of PapaParse's CSV parsing and unparsing features. Interested in integrating this with Angular JS - anyone able to assist with this integration? Excited about incorporating PapaParse into an Angular environment. Work ...

Overlap with upper picture formatting

I've been struggling to create an ion-card with two images: a main picture and a small book cover. Any ideas on how to achieve this? Note: The layout should have 2 images, one at the top and another as a small book cover. Check out this sample on St ...

When attempting to activate the same route, the Angular 8 router fails to trigger any events when utilizing `onSameUrlNavigation: 'reload'`

Router configuration: imports: [ RouterModule.forRoot(routes, { onSameUrlNavigation: 'reload', scrollPositionRestoration: 'top' }) ], Defined route: { path: 'orders', loadChildren: './ ...

Properly managing mouseover events on a flipped div: tips and tricks

I created a div that flips when clicked using some HTML and CSS code. It works perfectly in Firefox 39 and Chrome 43. Here is the markup: <div class="flip-wrapper flippable-wrapper" id="fliptest-01"> <div class="flip-wrapper flippable ...

What is the method for customizing the hover color in a mantine.ui menu?

I've been attempting to modify the menu color when hovering over it, but unfortunately, it's not working. Could anyone provide guidance on how to change the hover color in mantine.ui menu? ...

Fostering direct communication among sibling directives

Is there a way for two sibling directives to communicate without using parent/children directive communication? The first select is filled through a REST service, and upon selecting a value, the second dropdown should be enabled and display a subset of opt ...

Create an array of various tags using the ngRepeat directive to iterate through a loop

I'm familiar with both ngRepeat and forEach, but what I really need is a combination of the two. Let me explain: In my $scope, I have a list of columns. I can display them using: <th ng-repeat="col in columns">{{ col.label }}</th> This ...

Implement video.js within an Angular 2 application

I've been attempting to use video.js for my angular2 videos, but I've hit a roadblock. I have included the video.js CDN in my index file. <link href="https://vjs.zencdn.net/5.11/video-js.min.css" rel="stylesheet"> <script src="https://v ...

Issue with Angular UI Router arises when state cannot be resolved upon page reload

I'm currently facing an issue with routing that I mentioned in the title. Even though my route is functioning, it encounters difficulties when the page is reloaded. Below is the routes object: { state: 'locations', config: { ...

Angular binding of variable names beginning with numbers

Currently in my Angular 4 project, I am utilizing the interpolation syntax below: {{variable.24h_volume}} Upon implementation, an error is thrown stating: Unexpected token '0.24' at column 5 in [{{variable.24h_volume}}] in ng:///AppModule/Comp ...

Validation in Angular form isn't triggering for the author input field

An issue has been observed with Angular form validation when it comes to binding the condition for the author field. Specifically pertaining to Angular 7 HTML: <div class="form-group"> <label class="col-md-4">Author Name </label> ...

What is the best way to upload an object in React using fetch and form-data?

Currently, I am facing an issue where I need to send a file to my express app as the backend. The problem lies in the fact that my body is being sent as type application/json, but I actually want to send it as form-data so that I can later upload this file ...

What steps can be taken to resolve the error message "AttributeError: 'WebElement' object does not have the attribute 'find_element_class_name'?"

try: main = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "main")) ) articles = main.find_elements_by_tag_name("article") for article in articles: header = article.find_element_c ...

What is the best way to retrieve user groups in Django?

My goal is to investigate the user groups associated with a user in Django. However, when I check the console, I encounter the message: Uncaught ReferenceError: user is not defined at 6/:643:33 The purpose of the function is to redirect the user based ...

How to efficiently structure CSS columns using Div elements within a Bootstrap card

<!-- New Blog Post --> <div class="card mb-4"> <img class="card-img-top" src="https://ichef.bbci.co.uk/news/660/cpsprodpb/948D/production/_109592083_mediaitem109591445.jpg" alt="Card image cap"> ...

When I set my header as "fixed", it automatically adjusts the size of the header box

Looking to create a fixed-header that includes clickable links on the left and our logo on the right. Encountering an issue when trying to make the header take up the full width of the screen in its fixed position. The header's width seems to shrink ...