The parent view in AngularJs navbar should remain active when any child view is currently active

My goal is to have the first view based on some sub-views while the main view remains abstract and only redirects to the first sub-view. I have implemented two nav bars for navigation - one for normal views and another specifically for navigating within the home sub-views. However, I am unsure how to set up the main navbar so that it is active when any of the home sub-views are active.

Below is the code for the top bar navbar. I want the Home button to be active when any of the sub-views of home (home.a, home.b) are active. Currently, the Home button always remains inactive.

<ul class="nav navbar-nav">
    <li ui-sref-active="active"><a ui-sref="home">Home</a></li> //I can use home.a here, but once I navigate to home.b, it becomes inactive
    <li ui-sref-active="active"><a ui-sref="about">About</a></li>
    <li ui-sref-active="active"><a ui-sref="contact">Contact</a></li>
</ul>

The sub-views navbar works correctly as the correct button is active when I am on a specific sub-view.

<ul class="nav nav-pills nav-stacked">
  <li ui-sref-active="active"><a ui-sref="home.a">Common</a></li>
  <li ui-sref-active="active"><a ui-sref="home.b">My</a></li>
</ul>

Currently, my setup allows the main navbar to be selected when the sub-views are active. However, if I change the main view to "contact," I cannot go back to the home because it is an abstract view:

.state('home', {
  abstract: true,
  templateUrl: 'app/main/main.html',
  controller: 'MainController',
  controllerAs: 'ctrl'
})
.state('home.a', {
  url: '/',
  templateUrl: 'app/main/a/a.html',
  controller: 'AController',
  controllerAs: 'ctrl'
})
.state('home.b', {
  url: '/',
  templateUrl: 'app/main/b/b.html',
  controller: 'BController',
  controllerAs: 'ctrl'
})
.state('contact', {
  url: '/contact',
  templateUrl: 'app/contact/contact.html',
  controller: 'ContactController',
  controllerAs: 'ctrl'
})

Answer №1

To resolve this issue, consider updating the active condition to reference the state object and modifying the ui-sref to use the href of the child path:

<li ng-class="{active: ctrl.$state.includes('home')}"><a href="/">Home</a></li>

Although this solution works, it lacks elegance. Are there any alternative approaches?

Answer №2

Hey there, it might be worth a shot to give this a try!

<div ui-sref-active="{'active': 'admin.**'}">
  <a ui-sref-active="active" ui-sref="admin.roles">Roles</a>
</div>

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

Is it possible to retrieve 2 arguments within a function in a non-sequential manner?

Let's say there is a function with arguments A, B, C, D, and E. Function(A, B, C, D, E) However, not all arguments are needed all the time. For instance, only A and C are needed in some cases. Currently, I would have to call the function like this: Fu ...

using Typescript in an Angular2 application

Can anyone explain why I am receiving an error when attempting to declare a const? The error message states: Expecting new line or semicolon export class MyClass{ const ALLOC_INVESTORS = "Allocation Investors"; } ...

What could be causing the malfunction when using TypeScript's generic T with any?

The playground demo The diff: A: function<T extends { status: number }> () {} B: function<T = any> () {} the B does not have access to T's property, while the A function only accesses T's status. Thank you ...

Troubleshooting navigation problems in AngularJS Bootstrap Navbar

Currently, I am working on integrating AngularJS with Bootstrap3 for a mobile application. In my index.html file, I have added a navbar (navbar-fixed-top) and there are forms loaded through partials/myform.html. However, when I scroll the page and a textbo ...

Using nested AngularJS functions in Firefox for enhanced functionality

Check out this simple fiddle I've created. It should display an alert when you press enter in the textbox. The functionality works in the latest versions of IE and Chrome, but not in Firefox: fiddle Here's the HTML code: <div ng-app="app" ng ...

What steps should I take to resolve the Heroku TypeScript and Node.js build error I'm experiencing?

This is my first time using Heroku. I encountered multiple errors in the Heroku logs when trying to deploy my project: 2023-02-03T09:02:57.853394+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=tech- ...

Is React Typescript compatible with Internet Explorer 11?

As I have multiple React applications functioning in Internet Explorer 11 (with polyfills), my intention is to incorporate TypeScript into my upcoming projects. Following the same technologies and concepts from my previous apps, I built my first one using ...

Utilizing Observable to dynamically bind ngClass in Angular

I currently have a container with the following structure <mat-sidenav-container [ngClass]="{'sidepanel-opened': ((isSidePanelVisible$ | async) as isSidePanelVisible) == true }"> </mat-sidenav-container> I am trying to u ...

Bring in the type "Request" from the express-request-id module

Within our web app that utilizes TypeScript and express, we integrated the npm package express-request-id to enhance our HTTP Requests and responses by adding X-Request-Id headers. We crafted a middleware to assign the request's id to the HTTP respon ...

Observe the task while returning - Firebase Functions

I am working on a Firebase Cloud Functions project where I need to run a scraping task within one of the functions. While the scraping is in progress, I also need to provide progress updates to the consumer. For example, informing them when the task is at ...

There was a Runtime Error that occurred, stating a TypeError: It is not possible to access properties of an undefined value (specifically

I've encountered an issue with a donut chart implemented from react-apex charts. Every time I try to render the page containing the chart, an error occurs. However, if I make changes to a property of the chart, it renders without any errors on the fro ...

Retrieving HTML in Angular

As I delve into the world of Angular, my goal is to retrieve an HTML template from a file using JavaScript without relying on ng-include or any other directives. The challenge lies in not having HTML elements to work with, as all I have is a JavaScript fil ...

The ng-app feature is causing the script to run endlessly

Currently, I am troubleshooting an issue within my angular application that is built on the asp.net web application empty template. The problem arises when I utilize ng-app; if I leave it blank, the $routeProvider fails to initialize. However, if I specify ...

Error encountered: React Typescript does not support the "any" type in a template literal expression

I have been encountering an issue with my slider component in React Typescript. The error message I keep getting is related to the "Invalid type 'any' of template literal expression" specifically at the const fillWidth variable. I am struggling t ...

Cross-domain cookie not being established - the complication between AngularJS and Node.js/Express

When making a cross-domain request, the cookie is not set. My server is running on localhost:8000 and the client side is on localhost:9000. The CORS settings on the Node.js/Express server are as follows: app.use(function(req, res, next) { console.log(req. ...

Unable to locate the "fcm-node" module in Node.js with TypeScript

When working on a TypeScript project, I usually rely on the fcm-node package to send Firebase push notifications in Node.js. However, this time around, I faced an issue. I know that for TypeScript projects, we also need to install type definitions (@types ...

Exploring FileReader and DOMParser for AngularJS applications

I have a user uploaded file using AngularJS and would like to manipulate the file contents using XML. Unfortunately, I am facing an issue with the DOMParser recognizing the text file. index.html <div ng-controller = "myCtrl"> <input type ...

Ways to utilize Subjects for sharing global information in Angular 6

I have been struggling to find an effective way to share data between two components that have the same parent in an Angular application. Currently, I am working with an Angular Material stepper where Step 1 contains one component and Step 2 contains anot ...

I have a JavaScript code stored as a string that I need to transform into plain JavaScript

If I have a variable in string format, for example suppose there is a JavaScript code inside it: var string="function myFunction(a,b){return a*b;}"; I want to convert this into pure JavaScript code format like so: function myFunction(a, b) { return ...

Order child property in AngularJSWe are sorting properties in

Greetings! I am in search of a way to arrange an angular list according to a specific child property. Here is the model that I currently have: $scope.data = [{name:"John",type:{talent:"genius",val:99}}, {name:"Paul",type:{talent:"genius", ...