Is there another method to activate a button dynamically if the Router Link Active is not functioning in the given scenario?

Snippet of HTML code

<div class="card-header">
  <h5>Refill by date</h5>
  <div class="card-header-right">
    <nav class="navbar bg-faded ">
      <ul class="nav navbar-nav">
        <li class="nav-item" routerLinkActive="active">
          <button (click)="setView('branch'); getFranchiseRefillsByDateAndBranch()" class="btn btn-mini btn-bg-c-blue btn-outline-default btn-round btn-action">
          <i class="icofont icofont-ui-check"> By branch</i>
          </button>
        </li>
        <li class="nav-item" routerLinkActive="active">
          <button (click)="setView('product'); getFranchiseRefillsByDateAndProduct()" class="btn btn-mini btn-bg-c-blue btn-outline-default btn-round btn-action">
          <i class="icofont icofont-ui-check"> By product</i>
          </button>
        </li>
        <li class="nav-item" routerLinkActive="active">
          <button (click)="setView('list'); getFranchiseRefillsByDateAndList()" class="btn btn-mini btn-bg-c-blue btn-outline-default btn-round btn-action">
          <i class="icofont icofont-ui-check"> Refill list</i>
          </button>
        </li>
      </ul>
    </nav>
  </div>
</div>

Segment of TypeScript code

  getFranchiseRefillsByDateAndBranch() {
    this.items = [];
    var hrchyIds = this.jwtHelperService.getFRefillHrchyIds();
    var ym = this.localStorage.retrieve('frym');
    if (hrchyIds) {
      this.refillService.getFranchiseRefillsByDateAndYear(hrchyIds, ym, "branch", this.page, this.rec).subscribe(val => {
        this.items = val;
      });
    }
  }

CSS snippet for styling

.nav-item.active button i{
    color: #3D94CD !important;
}

I'm attempting to modify the color of the active button using router link active, but this approach isn't effective in this scenario. Is there an alternative method to dynamically add a class based on the current click?

Answer №1

When a routerLink is present on an element, routerLinkActive will function correctly.

In the TypeScript file, maintain a variable called currentActiveItem and assign it a default value like "branch".

currentActiveItem : string = "branch"

Modify the value of this variable in the setView function.

Furthermore, utilize ngClass on corresponding li elements as shown below.

[ngClass]="{'active' : currentActiveItem == 'branch'}"
[ngClass]="{'active' : currentActiveItem == 'product'}"
[ngClass]="{'active' : currentActiveItem == 'list'}"

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

Strategies for effectively searching and filtering nested arrays

I'm facing a challenge with filtering an array of objects based on a nested property and a search term. Here is a sample array: let items = [ { category: 15, label: "Components", value: "a614741f-7d4b-4b33-91b7-89a0ef96a0 ...

Shift a Div to the left prior to stretching it out

I am trying to achieve a smooth leftward movement and expansion animation for a div. Currently, I am experimenting with class switching on click events to transition between the desired states. However, I am facing difficulty in making the element first mo ...

Styling with Chemistry: CSS for Chemical Formulas

Is there a way to write multiple formulas in CSS, or is there an alternative method I should consider? I want to integrate these formulas on my quiz website for students to use. I came across some intriguing examples that could be useful: However, I am s ...

JavaScript/CSS manipulation: The power of overriding animation-fill-mode

When animating text using CSS keyframes, I use animation-fill-mode: forwards to maintain the final look of the animation. For example: #my-text { opacity: 0; } .show-me { animation-name: show-me; animation-duration: 2s; animation-fill-mod ...

Ways to position the navigation menu alongside a search bar and a dropdown tab?

My navigation bar includes a search form, multiple links, and a dropdown link leading to additional links. Unfortunately, I'm struggling to align everything on the same line within the navbar. This is the current output: View My HTML Page Below is ...

Replicate and $(document).ready()

My form has required fields that need to be completed. To alert users about blank fields, I have implemented the following code: $(document).ready(function() { $('input.required:text').focus(function() { $(this).css({'background ...

What sets apart the function from the `=>` in TypeScript?

Currently, I am diving into the world of TypeScript and finding myself puzzled by the distinction between the keyword function and => (fat arrow). Take a look at the code snippet below: interface Counter { (start: number); interval: number; ...

Tips for resolving the error "Binding element has no default value and initializer provides no value in TypeScript"

I am currently in the process of converting a JavaScript Apollo GraphQL API project to TypeScript. During this migration, I encountered an error related to a user code block: var idArg: any Initializer provides no value for this binding element and the ...

Positioning of SVG text along the y-axis

https://i.sstatic.net/FkBRo.png In my project, I am creating a population pyramid using d3 in combination with react. While d3 handles the calculations, react is responsible for rendering the DOM elements. Everything is going smoothly so far, except for p ...

"Removing the default background color in the latest version of next.js: A step-by

Check out this screenshot for reference Whenever I change the gradient color in next.js, it defaults to these lines. However, switching to a blue or green color fixes the issue. The problem arises when using my current background color code: background: ...

Execute a function in Angular2 every 10 seconds

I've been working on a project that requires a Timer to make an API call every 10 seconds. I tried using setTimeout, but encountered an issue where it turned into an infinite loop. Even when navigating to another page, it continued to execute the if c ...

Launching a nested route with a specific URL in Angular 5

I've set up specific routes in my application: export const ContainerRoutes: Routes = [ { path: 'container', component: containerComponent, children: [ ...FirstRoutes, ...SecondRoutes ...

Removing HTML Tags in Ionic - A How-To Guide

After utilizing Ionic 3 to retrieve data from a WordPress API, I am encountering an issue with displaying the content on the app UI. The problem arises from the presence of HTML tags within the content being printed along with the text. While seeking solut ...

Troubleshooting problem with CSS combinators

I'm having trouble with this, if I have these two tag combinations in my CSS: aside h4 { color:yellow; } article h4 { color:blue; } And I apply them to the following HTML: <div> <h4>International new</h4> ...

Leveraging a Static HTML landing page template within an Angular 6 application

I currently have an Angular 6 app hosted on example.com, with the dist folder being served on port 5000. Additionally, I have a separate landing page folder with its HTML, CSS, and JS files being served on port 6000 at example.com/welcome. Is there a way ...

Tips for positioning the sign up button in the navbar

My expertise lies in HTML, but I'm currently honing my CSS skills. I'm facing a challenge with positioning my sign-up button at the extreme right without it being part of the main navigation link section. Does anyone have suggestions on how to ac ...

Table for maximizing space within a cell (Internet Explorer 8)

I am attempting to make the "innerTable" fill up all available space within another table. The issue is with IE8 (no compatibility mode, IE8 browser mode, Doc mode IE8 Standards). The table does not expand to fit the containing TD. I tried enclosing the ta ...

Using ngModel conditionally in Angular 2

I am facing a requirement where I need to bind a phone number to ngModel only if it is present. The code I am using is as below: <cd-input size="15" [(ngModel)]="phone_numbers[0].full_number" [reformat]="something" [format]="something" placeh ...

Start up a server using Angular along with Node.js and Express framework

I am encountering an issue with configuring Express as a server in my Angular application. The app loads without any issues when accessing the HOME route, but when trying to access another route, I receive an error message: Cannot GET / This is how I hav ...

Enhance your website with Bootstrap 4 by separating text and images in your carousel

I've been struggling to create a slider similar to the one shown in the image. I attempted using the Bootstrap carousel, but ran into issues with inconsistent image sizes and the text on the left side jumping around before settling in its correct posi ...