Angular fails to combine values within routerLink

The issue is straightforward - I have a component that retrieves the last searched items saved in sessionStorage as an array of ListItem objects:

export class SearchlistComponent {
   results = JSON.parse(<string>sessionStorage.getItem("lastSearch"));

}

The HTML displays these items with corresponding links for routing:

<ul class="list-group mt-3">
 <li class="list-group-item" *ngFor="let result of results">
   <div *ngIf="result.itemType == 'hospital'">
     <div  routerLink='/hospital/{{result.itemId}}'>
       <h4>{{result.itemName}}</h4>
       <h5>{{result.itemLocation}}</h5>
     </div>
   </div>
   <div *ngIf="result.itemType == 'ward'">
     <div routerLink='/ward/{{result.itemId}}'>
       <h4>{{result.itemName}}</h4>
       <h5>{{result.itemLocation}}</h5>
     </div>
   </div>
   <div *ngIf="result.itemType == 'medic'">
     <div routerLink='/medic/{{result.itemId}}'>
       <h4>{{result.itemName}}</h4>
       <h5>{{result.itemLocation}}</h5>
       <h6 *ngIf="result.itemType == 'medic'">{{result.itemDescription}}</h6>
     </div>
   </div>
 </li>

While the list appears correctly, clicking on an item does not attach the value to the route. For example, instead of '/hospital/hospitalId', it only shows '/hospital'.

This issue arises specifically when running the site with SpringBoot, but not with ng serve. I've verified that data gets stored and retrieved properly. What could be causing this problem?

Answer №1

RouterLink requires an array as a value in this format:

[routerLink]="['user', user.id, 'details']"
= "/user/8/details" for example.

<ul class="list-group mt-3">
 <li class="list-group-item" *ngFor="let result of results">
   <div *ngIf="result.itemType == 'hospital'">
     <div [routerLink]="['/hospital', result.itemId]">
       <h4>{{result.itemName}}</h4>
       <h5>{{result.itemLocation}}</h5>
     </div>
   </div>
   <div *ngIf="result.itemType == 'ward'">
     <div [routerLink]="['/ward', result.itemId]">
       <h4>{{result.itemName}}</h4>
       <h5>{{result.itemLocation}}</h5>
     </div>
   </div>
   <div *ngIf="result.itemType == 'medic'">
     <div [routerLink]="['/medic', result.itemId]">
       <h4>{{result.itemName}}</h4>
       <h5>{{result.itemLocation}}</h5>
       <h6 *ngIf="result.itemType == 'medic'">{{result.itemDescription}}</h6>
     </div>
   </div>
 </li>

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

Categorizing the types of dynamically retrieved object values

I've developed a unique class with two properties that store arrays of tuples containing numbers. Additionally, I've implemented a method called "getIndex" which dynamically accesses these properties and checks for duplicate number values within ...

A service worker of unknown origin is currently getting registered

Currently, I am using the service worker provided in create-react-app. After registering it in index.tsx with serviceWorker.register();, everything seems to be working fine. However, upon closer inspection in the dev tools' Application tab, I noticed ...

The XML-BEANS compiled schema was not found at the specified resource path for org/apache/poi/schemas/ooxml/system/ooxml/ctstring4cdatype.xsb

Struggling with SpringBoot to create a xlsx file with pivots, but it keeps crashing with this specific stacktrace. Utilizing xmlbeans version 5.0.3 XML-BEANS compiled schema: Unable to find compiled schema resource org/apache/poi/schemas/ooxml/system/oox ...

Building a recursive component in Angular 2 using templates

If you want to check out the complete proof of concept, click on this link: https://plnkr.co/edit/slshjP?p=preview I am aiming to develop a straightforward tree component that empowers users to define a template for each node like so: <app-tree-editor ...

Pages in Angular are being loaded multiple times when they are loaded with various parameter values

I have encountered an issue with my page setup that involves contracts and chats. Each chat corresponds to a specific contract, and depending on which chat or contract is open, the URL changes accordingly: david/contracts -> david/contracts/12 david/c ...

Can APP_INITIALIZER function within lazy loaded modules in Angular?

I recently implemented a lazy loaded module in my application and tried to add APP_INITIALIZER but unfortunately, it doesn't seem to be triggering. Surprisingly, the syntax I used is identical to that of my main app where it works perfectly fine. Can ...

I want to search through an array of tuples to find a specific value in the first index, and if there is a match, I need to return the value in the second index of the matching tuple

I am dealing with an array of tuples: var tuparray: [string, number][]; tuparray = [["0x123", 11], ["0x456", 7], ["0x789", 6]]; const addressmatch = tuparray.includes(manualAddress); In my function, I aim to verify if the t ...

How can I retrieve query parameters in the Server app directory of Next.js 13 using a function?

I am looking to retrieve a query token from localhost/get/point?token=hello. import { NextResponse } from 'next/server' import base64url from 'base64url' type Params = { token: string } export async function GET(req: Request, contex ...

What is the process for updating an Angular application when a new dependency is added to the package.json file and how can it be used in

Currently, I am facing an issue while trying to utilize a new dependency in the package.json file and importing it into the component file. Despite attempting to install or update the dependency using npm, I encounter difficulties with the import process a ...

Is it possible to develop a C equivalent of the typescript Record type?

Is there a way to create a record type equivalent in C like what can be done in TypeScript, and add attributes during runtime? I am aiming to replicate the following: const familyData: string[] = ["paul", "em", "matthias", "kevin"]; const myFamily: Record ...

Best practices for stubbing an element in order to effectively unit test a LitElement component

Trying to perform unit tests on LitElement components has been quite a challenge for me. I found myself stuck when attempting to isolate components, particularly in figuring out how to stub elements effectively. The Polymer project provides some informatio ...

unable to expand navbar in Angular 5 project with Bootstrap 4

I am encountering an issue with my navigation bar in a project that uses Angular5 and Bootstrap4. The navigation bar is supposed to expand when the screen size is small, as indicated by the navbar-expand-sm class in Bootstrap4. However, the navbar remains ...

Typescript: Utilizing Index-Based Callback Parameters in Functions

I am currently working on creating a function that can handle specific data types ("resource") along with an array of arrays containing call objects and corresponding callbacks for when the calls finish ("handlers"). function useHandleResource< R exte ...

The function is unable to access the 'FacebookAuthProvider' property as it is undefined

I'm having trouble using Angular Firebase to connect with Facebook. I keep encountering this error: AppComponent.html:2 ERROR TypeError: Cannot read property 'FacebookAuthProvider' of undefined at AuthService.signInWithFacebook (auth.servic ...

Developing Angular applications with numerous projects and libraries in the build process

The other day I successfully deployed the initial version of our front-end application, but encountered some confusion during the build process setup. Many Angular apps do not utilize the workspace feature unless they are creating multiple standalone appli ...

Why won't the sound play on the button with the picture?

I am currently working on a website project that requires buttons with pictures and sound. Despite my efforts, the sound feature is not functioning properly in Chrome and Firefox. I am still learning and would like to know how to toggle the sound on and of ...

Angular 8 is throwing a NullInjectorError because it cannot find a provider for AngularFireAnalytics

After running 'npm test', I encountered the following error message: NullInjectorError: StaticInjectorError(DynamicTestModule)[ComparePageComponent -> AngularFireAnalytics]: StaticInjectorError(Platform: core)[ComparePageComponent - ...

Angular 16 routing not loading content

I configured the routes in Angular v16 and encountered a blank page issue with the login and register functionality. I suspect it has to do with routing, but after checking multiple times, I couldn't pinpoint the exact problem. Below are snippets of t ...

Encountering a 404 error when using the NestJS GET function within the service and controller

I am facing an issue with creating simple GET logic. When I test it in Postman, I keep receiving a 404 error. books.service.ts contains the following basic logic: constructor( @InjectRepository(Books) private readonly booksRepo: Repository<Boo ...

Why is Angularfire2 failing to save the data it fetches?

It seems that I am struggling to accomplish what I need because of a lack of knowledge on the proper method. My goal is to save a connection between a user and a school (which is also a user), and then retrieve some image URLs from the school user in a new ...