Add a border to the navigation bar item to indicate the current page being displayed

This section shows the current navigation bar item https://i.sstatic.net/6RAGJ.png

This is the desired outcome when clicking on the tab https://i.sstatic.net/8nFeB.png

Uncertain about the best approach for achieving this. I have attempted to submit a value to the navigate functions, set global values in SharedService, and then retrieve the values from SharedService to display using an ngIf statement in the HTML.

<div class="header-container">
<div class="container-fluid">
    <div class="row">

        <div class="col-md-12 col-sm-12">

            <nav class="navbar navbar-expand-sm navbar-light container">
                <a href='#' class="navbar-brand">
                    <img src="../../assets/images/logo.png" width="100px" height="20" alt="" loading="lazy">

                </a>
                <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
                    <div class="navbar-toggler-icon"></div>
                </button>

                <div class="collapse navbar-collapse" id="navbarMenu">
                    <ul class="navbar-nav ml-auto">
                        <li class="nav-item mr-3">
                            <a (click)="aboutShop()" class="nav-link">About SHOP</a>
                        </li>

                        <li class="nav-item mr-3">
                            <a (click)="workingWithUs()" class="nav-link">Work with Us</a>
                        </li>
                        <li class="nav-item mr-3">
                            <a (click)="pricing()" class="nav-link">Pricing</a>
                        </li>
                        <li class="nav-item">
                            <a (click)="help()" class="nav-link">Help</a>
                        </li>

                        <li class="nav-item d-none d-sm-block ml-4">
                            <a href="#" class="nav-link"></a>
                        </li>

                        <li class="nav-item">
                            <a (click)="login()" class="nav-link">Login</a>
                        </li>
                    </ul>
                </div>
            </nav>
        </div>

    </div>

</div>

import { Component, OnInit } from '@angular/core';
import {SharedService} from '../sharedService';
import {Router} from '@angular/router'
@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
  isHelp:false;
  constructor(
    private router:Router,
    private sharedService: SharedService,
    ) {
     }

  ngOnInit(): void {
  }

  login(){
    this.router.navigate(['home/login']);
  }
  aboutShop(){
    this.router.navigate(['home/about-us']);
  }
  workingWithUs(){
    this.router.navigate(['home/working-with-us'])
  }
  pricing(){
    this.router.navigate(['home/pricing'])
  }
  help(){
  this.isHelp =true;
    this.router.navigate(['home/help'])
  }
}

import {Injectable} from '@angular/core'
import { BehaviorSubject } from 'rxjs';

@Injectable()
export class SharedService{
    private _help;

    set help(value){
        this._help = value
    }

    get help(){
        return this._help;
    }
}

Here's an attempt I made

                            <li *ngIf="isHelp" class="nav-item" style="border-bottom: 1px solid;">
                            <a (click)="help()" class="nav-link">Help</a>
                        </li>
                        <li *ngIf="!isHelp" class="nav-item">
                            <a (click)="help()" class="nav-link">Help</a>
                        </li>

Answer №1

To start, you need to add an Input to the navbar component that will show which component is currently active.

navbar.component.ts :

@Input() activeTab : string;

Next, in the parent components, provide the respective tags as input to the navbar.

component1.component.html :

<app-navbar [activeTab]="'component1'"></app-navbar>

component2.component.html :

<app-navbar [activeTab]="'component2'"></app-navbar>

Based on the input tag, toggle a class on each navigation item.

navbar.component.html :

<li class="nav-item mr-3" [ngClass]="{'active' : activeTab=='component1'}">
    <a class="nav-link">Component1</a>
</li>
<li class="nav-item mr-3" [ngClass]="{'active' : activeTab=='component2'}">
    <a class="nav-link">Component2</a>
</li>

navbar.component.css :

.nav-item.active {
    border-bottom : 2px solid grey;
}

Check out the demo here: https://stackblitz.com/edit/angular-ivy-uyeb9r

Answer №2

You can experiment with the following in your CSS:

text_id:hover{border-bottom:1px solid black}
or apply it to the <li> element in the text.

Feel free to reach out if you need further assistance.

Answer №3

Experiment with incorporating the style border-bottom:1px solid grey into a class and applying it to every <li> element, along with assigning a property to indicate the currently active route.

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

HTTrack displays an error message indicating that the file cannot be located

After downloading a website using HTTrack with the command below: /usr/local/bin/httrack https://www.website.com -O /Users/mainuser/Desktop/website -n -j I located the index.html file in the website folder and attempted to run it. However, Chrome display ...

Implementing CSS Media Print, tables elegantly transition to a fresh page

I am encountering a challenge when it comes to printing my dynamic table along with some preceding text. It seems that sometimes the table begins on a new page, resulting in the header test being isolated on the first page and only displaying the table on ...

Can you customize the first element within a span tag using a for loop?

I am having an issue where only the last element in a span is being set within a for loop. The code is functioning correctly, but I want to ensure that the first element is the one set as cust_name. success : function(data) { co ...

What is the best way to determine if a child component's property has changed within a method?

The child component is app-google-maps-panel and has 2 properties in the parent component: <div class="col-6"> <app-google-maps-panel [longitude]="longitude" [latitude]="latitude"></app-google-maps-panel> & ...

synchronize the exchange of information and events between two components

Just joined this platform and diving into Angular 7 coding, but already facing an issue. I've set up two components along with a service. The data is fetched by the service upon clicking a button in the searchbar component, and I aim to display it i ...

a class instance in Java Script returned as undefined

Having sought resolution for this question once before, I find myself revisiting it as the issue still persists. My apologies for the duplicate post. The first file in question is block.js: class Block{ constructor(timeStamp, lastBlockHash, thisBloc ...

Retrieve an array from JSON encoding using AJAX

I have been attempting to retrieve 4 JSON arrays stored in a separate file and include them in my main file using an AJAX request. However, I'm facing difficulties storing these arrays into variables and logging them in the console. Here are the resul ...

Ways to switch classes within a loop of elements in vue.js

I'm just starting to learn vue.js and I'm working on a list of items: <div class="jokes" v-for="joke in jokes"> <strong>{{joke.body}}</strong> <small>{{joke.upvotes}}</small> <button v-on:click="upvot ...

Having trouble locating an element on a webpage? When inspecting the element, are you noticing that the HTML code differs from the source page?

I'm having trouble locating a specific element on a webpage. When I use the Inspect Element tool, I can see the HTML code with the element id = username, but when I check the page source, all I see is JavaScript code. Does anyone have any suggestions ...

When utilizing the "as" attribute, styled components do not inherit any styles

I am currently utilizing both styled-system and styled components, working on a simple scenario like the one below: const buttonFont = { fontFamily: "Chilanka" }; // define basic text styling const Text = styled.div` ${typography} ${color} `; // c ...

Use JavaScript to change the CSS pseudo-class from :hover to :active for touch devices

Looking to eliminate hover effects on touch devices? While it's recommended to use a hover class for hover effects, instead of the hover pseudo-class, for easier removal later on, it can be a challenge if your site is already coded. However, there&apo ...

Retrieving a JSON object using a for loop

I'm working on a basic link redirector project. Currently, I have set up an Express server in the following way: const express = require('express'); const app = express() const path = require('path'); const json = require('a ...

Issue updating Bootstrap in ASP.Net Core 2.1 with Angular Template

Every time I start a new ASP.Net Core 2.1 project with the angular template and try to update the bootstrap version from 3 to 4 in package.json, it ends up breaking my application. Despite numerous attempts such as deleting the node_modules folder and rein ...

elevate the div with a floating effect

My goal is to create a chat feature for users that will only be visible for a limited time period. I was thinking of using PHP and timestamp to achieve this functionality, but I also want to make the chat visually disappear by having the message div float ...

Set a value for the hidden field on the page using client-side scripting

When working with an ASP.net application, I often use Page.ClientScript.RegisterHiddenField("hf_Name", value). However, I am curious about how to override or set a new value for the same Hidden Field 'hf_Name' in the code behind. Can you provide ...

Connect your Nuxt.js application with Mailchimp for seamless integration

I've tried the solution recommended in this thread, but unfortunately, it's not working for me. Every time I run npm run dev, I encounter an error message: ERROR - Compilation failed with 1 error at 11:21:24 The following dependency was not fo ...

What steps can I take to ensure my customized tab component can successfully pass an index to its children and effectively hide panes?

I'm currently working on developing a custom Tabs component to enhance my knowledge of React, but I seem to have hit a roadblock. The structure involves three key components that draw inspiration from Material-UI tabs: Tabs, Tab, and TabPane. Tabs.j ...

Content is not being contained within a div container

I'm facing a unique issue that I've never encountered before - text is not wrapping inside a div element. Here's a snippet of my HTML code: http://jsfiddle.net/NDND2/2/ <div id="calendar_container"> <div id="events_container"& ...

expand the module's children routes by including additional options from another module

Imagine we have a module called "Parent" which has some child routes: const routes: Routes = [ { path: '', component: ParentComponent, children: [ { path: '/:id', ...

Include additional data in the FormData object before sending it over AJAX requests

Currently, I am utilizing AJAX to store some data. The primary concern I have is figuring out how to incorporate additional information into the FormData object along with what I already have. Below is the function that I'm using. Could you assist me ...