Is there a way to verify the selected navigation item in the console?

Need help with checking the selected navigation using console.log?

export const navItems: NavData[] = [
  {
    name: 'Book #1',
    url: '/book',
    icon: 'fa fa-book',
    children: [{
      name: 'Book #2',
      url: '/book',
      icon: 'fa fa-pencil'
    },{
      name: 'Shopping Plan',
      url: '/plan',
      icon: 'fa fa-shopping-cart',
    }]
  }
]

If you click on "Shopping Plan" in the sideNav, the console will display "Shopping Plan". If you click on "Book #2", the console will show "Book #2". In simpler terms, how can you log the selected navigation item to the console?

This is the HTML code for reference:

  <app-sidebar [fixed]="true" [display]="'lg'">
    <app-sidebar-nav [navItems]="navItems" [perfectScrollbar] [disabled]="sidebarMinimized"></app-sidebar-nav>
    <app-sidebar-minimizer></app-sidebar-minimizer>
  </app-sidebar>

Answer №1

From what I understand, achieving this functionality from a higher-level component may not be possible based on the code provided. To make this work, you would need to make modifications to the app-sidebar-nav component and add a (click) listener to the list items that contain the navItems. In the HTML structure of your app-sidebar-nav component, ensure it resembles the following by adding a (click) event:

<ul>
<li ngFor="let item of navItems" (click)="showInConsole(item.name)">{{item.name}}</li>
</ul>

Additionally, in your component file, include the following method:

showInConsole(name){
console.log(name);}

Answer №2

Here is an example of how I create HTML with an assigned (click) event:

<app-sidebar-nav 
  [navItems]="navItems" 
  [perfectScrollbar] 
  [disabled]="sidebarMinimized"
  (click)="onItemClick($event)">
</app-sidebar-nav>

In addition, I include a function in typescript to handle this event:

  onItemClick(e){
    console.log(e.srcElement.innerText);
  }

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

Using checkboxes for filtering in a React application

Here are the data sets I am working with: const dataSet = [ { id: 1, title: 'object1', published: true, }, { id: 2, title: 'object2', published: true, }, { id: 3, title: 'object3', ...

Utilizing the "row" and "column" attributes in Angular (Flexbox) results in significant spacing between input fields within the HTML code

I'm working on aligning 2 input fields side by side within the same line. To achieve this, I've been utilizing Flexbox. However, I've observed that when using Flex with both 'row' and 'column', it introduces extra spacing ...

You cannot assign void to a parameter expecting a function with no return value

I have been working on an angular application that was initially developed in Angular 2, then upgraded to versions 7 and 9, and now I'm attempting to migrate it to Angular 11. There is a function in my code that fetches the notification count for the ...

The assignment of type 'string' to type 'UploadFileStatus | undefined' is not permissible

import React, { useState } from 'react'; import { Upload } from 'antd'; import ImgCrop from 'antd-img-crop'; interface uploadProps{ fileList:string; } const ImageUploader:React.FC <uploadProps> ...

Can we replace node_module imports with a different module (e.g. swapping lodash with lodash-es)?

Currently in the process of upgrading from angular 9 to angular 10 within my mono-repo. Encountering numerous warnings like the ones below: WARNING in \libs\global-search\src\lib\components\status\status.component.ts depe ...

Building a TypeScript Rest API with efficient routing, controllers, and classes for seamless management

I have been working on transitioning a Node project to TypeScript using Express and CoreModel. In my original setup, the structure looked like this: to manage users accountRouter <- accountController <- User (Class) <- CoreModel (parent Class o ...

Dealing with 'TypeError X is Not a Function' Error in Angular (TypeScript): Occurrences in Certain Scenarios and Absence in Others

Recently, I came across an odd issue in Angular 14 where a type error kept popping up. Although I managed to refactor the code and find a workaround, I'm quite intrigued as to why this issue is happening so that I can prevent it from occurring again i ...

Angular 8 - Utilizing External URL Routing

After creating a route that generates a token, I encountered an error when trying to retrieve the token from the URL: if (!this.session.token) { let urlToken= 'https://www.url-system.com.br//auth&redirect_uri=http://8000/api/res-token'; let r ...

What is the best way to convert the text on a button into another language?

Currently, I am facing an issue with translating a button using Angular. The translation appears to be outside of the button in question. Additionally, there is another problem that I have encountered. For reference, here is a sample: https://i.stack.i ...

What is the process for defining a series of tests for karma through code?

Currently, I am in the process of developing an Angular Test Explorer. The exciting part is that I can view all the tests and execute them collectively by leveraging the karma module as shown in this code snippet: public async runWithModule(): Promise&l ...

Switch up the item's background in the dropdown list within Kendo UI Angular

Looking for a solution to highlight text and change background color in a dropdown list based on certain conditions. I searched the official Kendo forum without success. Any guidance or suggestions on how to resolve this issue would be greatly appreciate ...

The declaration file for the datepicker module could not be located, even though the necessary types have been installed

I'm encountering an issue during production build on my Next.js project. The error message reads: Could not find a declaration file for module 'react-datepicker'. '../node_modules/react-datepicker/dist/index.js' implicitly has an ...

The panel header is clickable and overlaps with the header buttons

My panel component includes a header with a title and buttons positioned in the right corner. Currently, the downward arrow (chevron) is used to toggle the expansion/minimization of the panel's contents. When I attempt to make the header clickable to ...

Issue with routing in a bundled Angular 2 project using webpack

Having a simple Angular application with two components (AppComponent and tester) webpacked into a single app.bundle.js file, I encountered an issue with routing after bundling. Despite trying various online solutions, the routing feature still does not wo ...

Encountering a SyntaxError while utilizing framer-motion in Next JS

Currently, I am working with NextJS version 12.0.3 and integrating framer motion for animations into my project. However, regardless of the framer-motion library's capabilities, whenever I add a tag to any HTML element in my component, an error is tri ...

Issue with SvelteKit: PageData not being refreshed in API response after initial render

I am relatively new to working with Svelte and SvelteKit, and I am currently trying to fetch data from an API. I have followed the SvelteKit todo sample code, which works well for the initial rendering and when clicking on an a tag. However, I am facing an ...

Exploring how to utilize checkboxes in Angular Material for data filtering from a database. Encountering an issue with the error message "nameValue

When attempting to utilize checkboxes to filter data in a material table, I ensured there were no errors present on my terminal or console. However, upon checking a checkbox in the browser, an error message stating "nameValue.toLowerCase is not a function" ...

When moving to a different route inside the router.events function, the browser becomes unresponsive

Within my top navbar component, I have set up a subscription to the router events: this.router.events.subscribe(event => { if (event instanceof RoutesRecognized && this.authService.isLoggedIn()) { // additional custom logic here: ...

Creating a function that takes a second parameter inferred from a mapped type, depending on the first parameter given

Here is a snippet of code similar to the example provided: export enum Group { FOO = 'foo', BAR = 'bar', BIZ = 'biz' } interface Mapping extends Record<Group, any> { [Group.FOO]: {fooString: string; fooN ...

Do not generate authentication code in JHipster using the --skip-server flag

Is there a reason why the authentication part is lost when generating a project with '--skip-server'? yo jhipster --skip-server It seems that upon generating the project without the server, the authentication gets affected (on AJS/A2). Is thi ...