Display the div element only if the router is active and the navbar is visible

I am attempting to display a div underneath each link in a navbar when the page is active. I am looping through each link using *ngFor.

What I am trying to achieve looks like this:

https://i.sstatic.net/PpY66.png

However, what I am currently getting is this: https://i.sstatic.net/wzpzI.png

Below is my code:

component.html

<li class="nav-item mx-1" *ngFor="let link of links">
  <a class="nav-link" routerLinkActive="active" [routerLink]="link.url" [routerLinkActiveOptions]="{ exact: true }">
  {{link.nombre}}
  </a>
  <div *ngIf="getRouteActive()" class="bg-primary" style="height: 4px;"></div>
</li>

component.ts

getRouteActive() {
  return this.router.url === '/item1';
}

Answer №1

If you want to achieve the desired outcome, consider passing the url as a parameter to your function:

checkIfRouteActive(url) {
      // Assuming that the url already includes a forward slash
      return this.router.url === url;
    }

Afterwards, in your HTML template:

<li class="nav-item mx-1" *ngFor="let link of links">
      <a class="nav-link" routerLinkActive="active" [routerLink]="link.url" [routerLinkActiveOptions]="{ exact: true }">
      {{link.title}}
      </a>
      <div *ngIf="checkIfRouteActive(link.url)" class="bg-primary" style="height: 4px;"></div>
</li>

This approach should help you accomplish what you are aiming for.

Answer №2

Another option is to employ CSS for achieving the same effect:

.nav-item {
    div {
       visibility: hidden;
    }

   .active + div{
       visibility: visible;
   }
}

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 functionality of both P and H1 tags seems to be malfunctioning

Can someone help me with changing the font family and size of my paragraphs and headings (p and h1)? Any assistance would be greatly appreciated. Feel free to review my code for any other errors as it has been a while since I last coded. html, body { ma ...

Disorganized HTML Elements

If you visit diartefloral.tech and use a cellphone in the "Sobre Nosotros" section of the menu, the footer overlaps an image. I'm not sure how to fix this issue. The most relevant code is shown below; I am using Bootstrap. Previously, I had an issue w ...

Click event not triggering to update image

I'm currently working on a Codepen project where I want to use JavaScript to change the image of an element with the id "chrome". However, my code doesn't seem to be working as expected. Can someone help me troubleshoot and fix this issue? Your a ...

Display the search bar under the navigation using Bootstrap

When the menu is collapsed at 1200 width, I am trying to display navigation underneath it. However, in this state, the search bar appears on the right side even when the menu has collapsed. Additionally, using a div to separate both sections of navigation ...

the iframe is not properly displaying the embedded responsive page

The primary webpage incorporates the responsive mobile page created with the JQUERY SUPERSLIDES plugin. The mobile page appears correctly when viewed directly on an iPhone using Safari. However, when it is embedded in an iframe, it does not display as expe ...

Display an image with text that appears on hover along with a change in opacity

Is there a way to make the hover effect on an image display with opacity while keeping the text inside unaffected by the opacity style? The image is housed within an input tag, and when clicked, a bootstrap modal will appear with an input form. .img_wra ...

Popper.js failing to initialize during page load

After attempting to initialize Popper.js as shown below, I am encountering an issue where nothing is happening. $().ready(function(){ $('[data-toggle="tooltip"]').tooltip(); }); Interestingly, when I apply the same code to a button, it w ...

What steps should I take to resolve the error message "Error: srcmain.ts is not found in the TypeScript compilation?"

I've exhausted all possible solutions on StackOverflow and have even gone as far as uninstalling both Node and Angular three times in the span of three days. I'm completely stumped as to why this issue keeps occurring specifically when using "ng ...

What is the best way to split a semicircular border radius in two equal parts?

Is there a way to halve the yellow line or remove it from above the red box, while keeping it below? Can this be achieved using just HTML and CSS, or is JavaScript necessary? * { margin: 0; padding: 0; box-sizing: border-box; } body { height: 1 ...

Having trouble getting the header div to span the entire screen

I'm currently working on a project where I have set the header div to 100% width and the main container to also be 100% width. However, I am facing an issue where the right and left sides of the header are not filling the entire space. I managed to fi ...

Tips for utilizing NgFor within Angular 2 to highlight an active item on a different HTML element

Utilizing NgFor for iterating over a collection: this.articles = [ { id: 1, title: “Tile 1“, summary: “First Article“ }, { id: 2, title: “title 2“, summary: “Summary 2“ }, ...

Switching between click and mouseenter in Angular 2+ is made easy with

I am currently working on developing a component for my application's menu. One of the requirements is that the sub-menus should open on mouseenter when the menu is in compact mode, and on click when it is in wide mode (both of these are defined as cs ...

Different Option for Nginx Server

Currently I am utilizing an AWS EC2 instance to host both my NodeJs backend and Angular frontend. Additionally, I am leveraging Route 53 for routing purposes and purchased a domain from GoDaddy. The hosting process involved the following steps: For the b ...

How to Fetch a Singular Value from a Service in Angular 4 Using a Defined Pattern

I am currently working on developing a service in Angular 4 (supported by a C# RESTful API) that will facilitate the storage and retrieval of web-application wide settings. Essentially, it's like a system for key-value pair lookups for all common appl ...

Deliver a distinct service instance for each component

I am facing a scenario where I need to have multiple instances of widget components on the page simultaneously. My goal is to ensure that each instance of the ContainerComponent has its own unique references to service instances. For instance, I aim for e ...

Simulating local storage functionality in Angular 7

Incorporating feedback from my reviewer, I made changes to my application that initially used local storage. Instead of directly accessing local storage, I created a reference to it which proved to work well. However, I encountered difficulty in mocking th ...

The Excel file fails to accurately interpret the date after being extracted from a zip folder

When uploading a zip folder containing an Excel file through the UI, I implemented the following code snippet: getZipData(file: File) { const jsZip = require('jszip'); let excelRecord: any[] = []; let images: { path: string, image: string }[] = [ ...

Error in Angular production build due to Clean CSS failure

Encountering the following error during the code build process, any solutions? > ng build --prod --no-aot --base-href 92% chunk asset optimizationD:\myproject\node_modules\@angular\cli\node_modules\clean-css\lib&bsol ...

What could be causing the empty space along the right edge of my website?

I've encountered an issue with my website where there's a white space on the right side of the page, causing horizontal scrolling. I attempted to resolve this by adding overflow: hidden to the CSS, which worked perfectly in Chrome. However, the p ...

Discover the steps to create a virtual scroll dropdown menu with Kendo in Angular

I'm dealing with a large data set obtained from an API. Instead of displaying all the data in the dropdown initially, I'd like to fetch the results from the server API as the user scrolls down through the dropdown menu. Could someone please prov ...