Highlighting the active menu item in Angular with Bootstrap

On the main page, I have the following code snippet for navigation:

<li ng-class="{ active: hello.isActive('/page1')}"><a ui-sref="root.content({store:hello.store_selected,product:hello.product_selected})" >page1</a></li>
<li ng-class="{ active: hello.isActive('/page2')}"><a ui-sref="root.turnover({store:hello.store_selected,product:hello.product_selected})">page2</a></li>

There are two controllers for each route: Page1Controller and Page2Controller. The main page, where this code is located, also has a controller named HelloController. In the helloController, I have defined the following function:

vm.isActive = function(viewLocation) {
  return viewLocation === $location.path();
};

When clicking on an item, it gets highlighted as expected. However, there are two issues: 1. Clicking anywhere else on the page removes the highlight from the current item. 2. At the start of the page or upon refreshing, no item is highlighted by default. Could you please provide some assistance? Thank you.

Answer №1

ui-router actually comes equipped with this feature already through the use of ui-sref-active

<li ui-sfref-active="active"><a ui-sref="root.content({store:hello.store_selected,product:hello.product_selected})" >page1</a></li>

Your issue might be that the path isn't an exact match and you may need to utilize indexOf() instead of simply checking for equality

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 error alert must be displayed directly beneath the specific box

When error messages occur in this code, they are displayed through an alert box. However, I would prefer to have the error message appear below the specific text box instead. This means that when I click on the submit button and a field is left empty, the ...

Using nested ternary operations in React can cause issues with accessing local variables

Note: I encountered an issue where the extra curly braces around the first ternary result did not solve my initial problem. I replaced them with parentheses. Josep's suggestion to use getTime required me to equate the dates. The Date().setHours(0, 0, ...

Is it possible to have both a Navbar and Sidebar concurrently using Bootstrap?

I have a slightly unconventional request - I want to incorporate the sidebar from along with the navbar-default example from http://getbootstrap.com/components/#nav. What would be the best approach to merge these two elements? Or is there a different pa ...

Utilizing one JavaScript file and consistent classes for multiple modals

Is it possible to have multiple modals using the same JS file, classes, and IDs? I created this code: <button id='myBtn'>Order/Discount</button> <div id='myModal' class='modal'> <div clas ...

Sophisticated method for efficiently managing and utilizing ngResource

Struggling with organizing services in my Angular app using ngResource. Any suggestions on how to streamline and simplify the structure? My controller declaration seems too heavy with services. /app/app.resource.js angular.module('totalizacao') ...

Choose an element from within a variable that holds HTML code

I have a specific div element that I need to select and transfer to a new HTML file in order to convert it into a PDF. The issue I'm facing is related to using AJAX in my code, which includes various tabs as part of a management system. Just to prov ...

Guide on displaying the value of an element in a Vue modal

I am working with a list of items displayed as <li> elements in a loop. When one of these elements is clicked, I would like a modal box to open up displaying specific content related to that particular element. The following data represents the item ...

leveraging third party plugins to implement callbacks in TypeScript

When working with ajax calls in typical javascript, I have been using a specific pattern: myFunction() { var self = this; $.ajax({ // other options like url and stuff success: function () { self.someParsingFunction } } } In addition t ...

Exploring the Component API in VueJS 3 with Typescript: Learn how to assign a class to a template ref

Is there a recommended way to add/remove CSS classes from a template ref using the Vue 3 Composition API and typescript? When trying to use modal.value, I encountered the following typescript errors: const modal = ref(null) results in Object is possibly ...

Guide on sorting an array within a specific range and extracting a sample on each side of the outcome

I need a simple solution for the following scenario: let rangeOfInterest = [25 , 44]; let input = [10, 20, 30, 40, 50, 60]; I want to extract values that fall between 25 and 44 (inclusive) from the given input. The range may be within or outside the inpu ...

Develop search bar button and file directory components

Within this application, there is a search engine designed to scan through the file tree for specific content. The goal is that upon entering a search query and clicking the Search button, the file tree will filter and display the relevant information. Cu ...

Using jQuery: What is the best way to implement form validation in jQuery before submitting?

I've created a Bootstrap form as shown below: <form id="loginForm" method="post" action="" role="form" class="ajax"> <div class="form-group"> <label for="userName"></label> <input type="text" class="form-co ...

Working with Angular: Dealing with ngRepeat and Empty Inputs

I am currently working on a project where I have an ng-repeat with a list of inputs. Interestingly, if the inputs are empty, the corresponding LI is hidden. <li ng-repeat="(key, value) in jewel" ng-if="value !== ''"> <input ng-mode ...

What is the best way to assign the value of an HTTP GET request to a subarray in Angular 8

Attempting to store data in a sub-array (nested array) but despite receiving good response data, the values are not being pushed into the subarray. Instead, an empty array is returned. for (var j=0;j<this.imagesdataarray.length;j++){ this.http.g ...

Alignment customization in Exceljs for richText cells

"exceljs": "^3.9.0" In order to create a spreadsheet with cells containing two different values, each aligned differently (one left and one right), I am looking for a solution similar to the image shown in this screenshot: https://i.ss ...

Idiosyncratic TypeScript behavior: Extending a class with comparable namespace structure

Lately, I've been working on creating a type library for a JavaScript written library. As I was defining all the namespaces, classes, and interfaces, I encountered an error TS2417 with some of the classes. I double-checked for any issues with method o ...

Is it possible to specify the timing for executing Typescript decorators?

One issue I've encountered is that when I define a parameterized decorator for a method, the decorator runs before the method itself. Ideally, I'd like the decorator to run after the method has been called. function fooDecorator(value: boolean) ...

Retrieve data from a ng-repeat variable within a controller

Below is the current code snippet: HTML <center><li ng-repeat = "x in items | orderBy: 'priority'"> <!-- color code priorities --> <span ng-style="cmplt" ng-class="{ red: x.type == &apo ...

The icon on the left displaying peculiar conduct

Currently in the process of developing an application using Bootstrap and incorporating a user feed for admins which is inspired by the Dark Admin theme. The structure of my HTML resembles the demo provided, with CSS styling being compared to ensure consis ...

Tips for sharing information through a JSON array

"advertisement_types":["ATM","Banner/Poster","Stalls"] Here is the HTML code: input(type='checkbox', value='Mobile/Communication Tower', ng-model='advertisement_types') | Mobile/Communication Tower ...