Struggling to figure out how to change the display when navigating between different routes

I've been struggling for the past 3 hours trying to switch between routes. Let me explain further:

Server Template HTML:

<!-- I want the first div to display when the component opens, but disappear and show router-outlet when a button is clicked.
     When switching servers, I want 'display' to be true -->
<div *ngIf="display">
    <h5>{{ server.name }}</h5>
<p>Server status is {{ server.status }}</p>
<button class="btn btn-primary" (click)="onEdit()">Edit Server</button>
</div>

<!-- The entire template should disappear when changing servers -->
<div>
    <router-outlet></router-outlet>
</div>

Server Component TS:

export class ServerComponent implements OnInit {
  server: {id: number, name: string, status: string};
  id: number
  display: boolean = true
  URLid: number

  constructor(private serversService: ServersService, private route: ActivatedRoute, private router: Router) { }


  ngOnInit() {
    // It captures the id in the url and searches for its respective server
    this.id = +this.route.snapshot.params['id']
    this.server = this.serversService.getServer(this.id)
    this.route.params.subscribe((params: Params) => {
      this.id = +this.route.snapshot.params['id']
      this.server = this.serversService.getServer(this.id)
    })
  }

  onEdit() {
    /* 
      When the button is clicked, display is set to false. This makes only 'router-outlet'
      displayed. However, when the server changes, everything disappears.
    */
    this.display = !this.display
    this.route.params.subscribe(params => {
      this.URLid = params['id']
    })
    this.router.navigate(['edit'], {relativeTo: this.route})

  }

}

EditServer Template:

<h4>No editing the server!</h4>

Other code is irrelevant.

If images help, here they are:

(1) Initial State of the problem https://i.stack.imgur.com/9d8GY.png

(2) Edit Server functionality https://i.stack.imgur.com/3gVYs.png

(3) Issue when changing to Devserver, it should reset to (1). https://i.stack.imgur.com/8Jupx.png

Any insights on what might be going wrong? Thanks!

Answer №1

Understanding the nested logic may be challenging at first glance. Nonetheless, implementing the following changes can help achieve the desired outcome (assuming correct interpretation):

Within the edit-server.component.ts file, locate the method named OnInit:

    this.route.params.subscribe((params: Params) => {
      this.id = +params['id'];
      this.server = this.serversService.getServer(this.id);

      // Include this line to reset the display to true when selecting a different server from the list
      this.display = true;
    });

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 Alert dialog in Shadcn will automatically close upon clicking the trigger from the dropdown menu

It seems like other people have encountered this issue, but they all used the alert dialog in the same file. I attempted to open the alert dialog using "" and included a dropdownmenuitem with 'delete' inside it. However, when trying to open the ...

Creating Dynamic ion-card Elements in Typescript by Programmatically Changing Values

Currently, I am working on a basic app that retrieves posts from the server and displays them as cards on the screen. At this early stage, one of my main challenges is figuring out how to dynamically add ion-card elements with changing content and headers ...

Filter the angular accordion by passing a simple array value into the input

I am looking to filter my array of accordion items based on the value of the question matching the input I provide. I have tried using the filter method for this. this.accordionItems = [ { "topic":"polizze", " ...

What is the best way to retrieve the final value stored?

This is how I am using my Selector:- private loadTree() { this.loading = true; this.store.select(transitionListSelector).pipe().subscribe(data => { console.log(data); data.map(item => { console.log(item); this.tr ...

Angular is throwing a RangeError due to exceeding the maximum call stack size

Encountering a stackOverflow error in my Angular app. (see updates at the end) The main issue lies within my component structure, which consists of two components: the equipment component with equipment information and the socket component displaying conn ...

There was an issue encountered when trying to fill in the details for the Print Queue entity. Win32 error message: The name of the printer provided is

System.Printing.PrintQueueException: An error happened while populating the attributes for the PrintQueue object. Win32 error: The printer name is not valid. When I execute this code in Windows 10, I encounter this issue, although it works perfectly in Wi ...

Using JavaScript, generate an array of objects that contain unique values by incrementing each value by 1

I have an array of objects called arrlist and a parameter uid If the property value has duplicate values (ignoring null) and the id is not the same as the uid, then autoincrement the value until there are no more duplicate values. How can I achieve the a ...

What is the best way to activate ngModelChange for a custom input field?

I have developed a custom input component and here is the code: html: <input class='ctrl__counter-input' maxlength='10' type='text' [value]="counterValue" [ngModel]="counterValue" (ngModelChange)="onKey(in ...

Angular2 error: Unable to access property 'getKey' because it is undefined

I am attempting to retrieve JSON data and store it in an Angular 2 object. Below is my main class app.component.ts: import {Component, NgModule} from '@angular/core'; import {Http, Response} from '@angular/http'; import 'rxjs/Rx&a ...

Dealing with the error "Type 'date[]' is not assignable to type '[date?, date?]' in a React hook

I'm attempting to assign a date range but encountering an error that states: Type 'Date[]' is not assignable to type '[Date?, Date?]'. Types of property 'length' are incompatible. Type 'number' is not assignab ...

The Angular 2 and TypeScript error stating that the argument of type response is not assignable is causing issues

In my Angular application, I have a service that includes a value defined as: private client_comments = new BehaviorSubject([]); When attempting to update this value with the response from an HTTP request, I encountered the following error message: A ...

Improper application of Angular bindings in Chrome when navigating to a page using the browser's back button

Have you encountered this issue before? I have a few textboxes generated within an ngFor loop: <tr *ngFor="let tableRow of lineItems; trackBy:trackByIndex; let rowIndex = index; "> <td class="psavingsSmallerGridCell"><input c ...

Angular NodeJS API failing to retrieve search data

I have implemented a search feature using Angular and Node.js. After testing it with Postman, everything seemed to be working fine. However, when I tried connecting it to the front-end Angular application, I encountered an error. Failed to load resource: t ...

Having trouble with ion-item click not functioning in Ionic 3?

Working on my Ionic 3 project, I have encountered an issue with the ion-item click listener. The scenario is that I am adding text over a canvas and then attempting to change the style of the text (such as font-family, font-size, bold, and italic). The UI ...

When upgrading from ng15 to ng16, beware of the error message stating that the type '(event: RouterEvent) => void' cannot be assigned to type '(value: Event_2) => void.'

section, I encountered issues with my Angular project after upgrading from ng15 to ng16. Specifically, errors are arising when trying to implement the code snippet below. Can anyone provide insights on what may be causing problems with the event argument ...

Submitting an array of objects via HTTP PUT

Struggling to find a way to update a list of Objects on MongoDB in one method call within the Submit button. Spent all day yesterday attempting, but it seems to be unsuccessful. The goal is to update all product sells in the database by entering values for ...

The parameter 'NextApiRequest' cannot be assigned to the parameter 'Request'

I encountered a typescript issue that states: Argument of type 'NextApiRequest' is not assignable to parameter of type 'Request'. Type 'NextApiRequest' is not assignable to type '{ url: string; }'. Types of pro ...

Establish a comprehensive background for your Angular project

I've been struggling to figure out how to set a background image for my entire angular project. I've tried using global css and the app.component.css file, but it only sets the background for the component area. Here is a snippet from my global ...

Certain Material-UI components appear to lack proper styling

I found a tutorial on how to incorporate material UI into my app at this link: https://mui.com/material-ui/getting-started However, I noticed that some components are not styled as expected and customizing the theme seems to have no effect... This is how ...