I am using angular-ui-router
and nested states in my application, and I also have a navigation bar. The nav bar is hand written, and uses ui-sref-active
to highlight the current state. It is a two-level navigation bar.
Now, when I am in, say Products / Categories
I would like both Products
(in level 1) and Categories
(in level 2) to be highlighted. However, using ui-sref-active
, if I am in state Products.Categories
then only that state is highlighted, not Products
.
Is there some way to make Products
highlight in that state?
A single uiSrefActive can be used for multiple uiSref links. This can be used to create (for example) a drop down navigation menu, where the menui is highlighted if any of its inner links are active. The uiSrefActive should be placed on an ancestor element of the uiSref list.
Angular UI-Router is a module that helps us create single-page applications. It provides an abstraction between the application and the browser by managing all of the routes in our application. It also helps make changes to our application in a more efficient way.
A ui-sref is a directive, and behaves similar to an html href . Instead of referencing a url like an href , it references a state. The ui-sref directive automatically builds a href attribute for you ( <a href=...> </a> ) based on your state's url.
Angular-UI-Router is an AngularJS module used to create routes for AngularJS applications. Routes are an important part of Single-Page-Applications (SPAs) as well as regular applications and Angular-UI-Router provides easy creation and usage of routes in AngularJS.
Instead of this-
<li ui-sref-active="active"> <a ui-sref="posts.details">Posts</a> </li>
You can do this-
<li ng-class="{active: $state.includes('posts')}"> <a ui-sref="posts.details">Posts</a> </li>
Currently it doesn't work. There is a discussion going on here (https://github.com/angular-ui/ui-router/pull/927) And, it will be added soon.
UPDATE:
For this to work, $state
should be available in view.
angular.module('xyz').controller('AbcController', ['$scope', '$state', function($scope, $state) { $scope.$state = $state; }]);
More Info
UPDATE [2]:
As of version 0.2.11
, it works out of the box. Please check the related issue: https://github.com/angular-ui/ui-router/issues/818
Here's an option for when you are nesting multiple states that are not hierarchically related and you don't have a controller available for the view. You can use the UI-Router filter includedByState to check your current state against any number of named states.
<a ui-sref="production.products" ng-class="{active: ('production.products' | includedByState) || ('planning.products' | includedByState) || ('production.categories' | includedByState) || ('planning.categories' | includedByState)}"> Items </a>
TL;DR: Multiple, unrelated, named states need to apply an active class on the same link and you have no controller? Use includedByState.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With