Implementing ngClass with a dynamic ID in the URL condition

I am attempting to create an ngClass condition that adds an active class to a list element. Specifically, I want it to work for both the URLs '/companies' and '/company/:id'.

The issue I am facing is that the current setup does not function properly for the '/company/:id' URL. How can I modify the condition so that it works for any 'id' instead of specifically ':id'?

<li class="nav-item" [ngClass]="{'active': menuActive === '/companies', 'active': menuActive.includes('/company')}" *ngIf="isLoggedIn">

Answer №1

When creating an object, it's crucial to avoid assigning the same property twice. If you find yourself in a situation where there is no predefined logic to follow, you can utilize code like this:

<li class="nav-item" [ngClass]="{active: menu === 'a' || menu === 'b' }"

To handle the active class conditionally in the HTML, you can modify the rule to check with the component:

<li class="nav-item" [ngClass]="{active: isActive(menu) }" *ngIf="isLoggedIn">

And within your component:

isActive(menu: string) {
  const menuWithId = /^\/companies\/[a-z0-9]+$/i;
  return menu === "/companies" || menuWithId.test(menu);
}

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

Guide on how to focus on a specific node programmatically within a PrimeNG Tree component

One of the features of PrimeNG is the ability to scroll to a specific TreeNode: To implement this in html: <p-tree #mytreeid id="mytree"></p-tree> In an Angular application: @ViewChild("mytree") mytree: Tree; // selection represents the Tre ...

Simplifying the process of implementing Jquery CSS Toggles

I am planning to simplify the process of toggling visibility for 12 different divs when clicking on 12 different buttons. Instead of repeating the same code multiple times, I am considering a more efficient approach. My idea is to: Step A) Initially hide ...

angular overlapping collapsing

Currently, I am developing a chat board application where users can leave comments under each post. I am in the process of creating a button that will collapse all the comments for better visibility, especially as they continue to grow. <div id="collap ...

Why is the mat-toolbar and mat-toolbar-row in the footer (shared) component of Angular 6 not showing up correctly, despite no error being reported?

Despite reviewing similar questions, I have not been able to find a solution for my specific issue. I have searched through the following links but failed to find a solution: Angular Material v6 Mat-Footer - "Cannot read property 'template' of ...

Ensure that the child element completely fills the parent element while maintaining a padding around the edges

How can I create a child div that fills 100% of the parent's width and height, including padding? I've tried using box-sizing = border-box, but it isn't working. I also attempted adding the box-sizing property to all elements with *, but tha ...

Show objects based on issuance date

I have implemented a function to retrieve a list of items from a feed URL as shown below: getFeed(){ return Observable.interval(5000) .mergeMap(() => this._http.get(this.feedUrl)) .map(this.extractFeeds) .catch(this.handleError ...

What is the best way to integrate an admin template into a website without relying on popular CMS platforms like WordPress?

Do I need to use a WordPress system in the backend in order to utilize an admin template? I've noticed that there are several sleek web admin templates available in Bootstrap. I'm interested in using one of them but unsure how to do so without re ...

Save the ID values of selected users in Angular Mentions feature

Using the angular mentions library, I successfully incorporated a textarea that enables me to mention multiple users. Is there a method to store the IDs of the selected users? Ideally, I would like to save them as an array of strings. For instance: If I ...

Updating the display in Angular 4 following modifications to an array

I am puzzled by a certain concept. I came across a notion that the view in my project only updates when some of the variables change their reference. However, I'm confused about how this applies to arrays. When I make changes to an array, sometimes th ...

Can you explain the distinction between using get() and valueChanges() in an Angular Firestore query?

Can someone help clarify the distinction between get() and valueChanges() when executing a query in Angular Firestore? Are there specific advantages or disadvantages to consider, such as differences in reads or costs? ...

Issues with compatibility between @ngui/auto-complete and ng2-daterangepicker in Angular 16

Recently, I upgraded my Angular project from version 11 to version 16. Within the project, I am utilizing @ngui/auto-complete and ng2-daterangepicker components. However, I encountered an issue when trying to import NguiAutoCompleteModule and Daterangepick ...

Generate the Ionic build and save it to the /dist directory instead of the /www

Every time I execute the command ionic build, it generates a fresh /dist directory containing all the same files that are typically found in the /www directory. Despite my attempts to make updates to the /www folder, only the /dist folder gets updated. Wha ...

Using background-image in Internet Explorer has always been a challenge

HTML <div id="banner" style="display: block; background-image: url('images/swirl_home.gif'); background-repeat: no-repeat; background-color: #e7e7e7;"> <div id="Hi"> some text here... </div> & ...

A guide on retrieving all the table data and displaying it in the user interface using templates in Django

I am working on a Django project where I have created a model named "Files" with fields such as Id (auto-generated) and file_name. My goal is to retrieve all file names from the database and display them as a list on the user interface. To achieve this, I ...

What could be causing flexbox not to shrink to fit after wrapping its elements?

My goal is to create a flexbox with a maximum width that allows elements to wrap beyond that width without affecting the overall size of the flexbox's "row." The issue I am encountering is that when an element stretches beyond the maximum width and w ...

Angular update row and save data to an array

When comparing the data of an edited row with the row just below it, I encounter a specific scenario. In a table containing 5 rows, as I edit records from top to bottom using the provided code snippet, I am able to store the values in an array. The most re ...

Creating a personalized tooltip in Angular for a bubble chart with ApexCharts

I'm currently working on customizing the tooltip for a bubble chart using the ApexCharts library. Here is the link to my project. ...

Drop and drag the spotlight

On my website, I am looking to implement a feature that will make it easier for users to identify the drag and drop area. I found a code snippet on JSFIDDLE that works perfectly there. However, when I tried to use it on my local server, it doesn't se ...

How come flex won't let me rearrange an image?

.nav { height: 500px; } .navphoto { height: 500px; display: flex; justify-content: flex-end; } <div class="nav"> <img class="navphoto" src="images/navphoto.jpg"> </div> Just wondering why I can't seem to move an image, bu ...

Issue: The element '[object Object]' is of type 'object', which is not supported by NgFor. NgFor only works with Iterables like Arrays. - Problem encountered in an Ionic Project

I'm currently working on retrieving my user's username from Firebase Firestore Database using Ionic and AngularFire. I have implemented the valueChanges() method to obtain the observable and am trying to process it using an async pipe. However, u ...