@@ -511,6 +511,17 @@ uiStateDirective = ['$uiRouter', '$timeout',
511511 * </div>
512512 * ```
513513 *
514+ * Arrays are also supported as values in the `ngClass`-like interface.
515+ * This allows multiple states to add `active` class.
516+ *
517+ * #### Example:
518+ * Given the following template, with "admin.roles" being the current state, the class will be added too:
519+ * ```html
520+ * <div ui-sref-active="{'active': ['owner.**', 'admin.**']}">
521+ * <a ui-sref-active="active" ui-sref="admin.roles">Roles</a>
522+ * </div>
523+ * ```
524+ *
514525 * When the current state is "admin.roles" the "active" class will be applied to both the `<div>` and `<a>` elements.
515526 * It is important to note that the state names/globs passed to `ui-sref-active` override any state provided by a linked `ui-sref`.
516527 *
@@ -543,9 +554,7 @@ uiSrefActiveDirective = ['$state', '$stateParams', '$interpolate', '$uiRouter',
543554 // Do nothing. uiSrefActive is not a valid expression.
544555 // Fall back to using $interpolate below
545556 }
546- if ( ! uiSrefActive ) {
547- uiSrefActive = $interpolate ( $attrs . uiSrefActive || '' , false ) ( $scope ) ;
548- }
557+ uiSrefActive = uiSrefActive || $interpolate ( $attrs . uiSrefActive || '' , false ) ( $scope ) ;
549558 setStatesFromDefinitionObject ( uiSrefActive ) ;
550559
551560 // Allow uiSref to communicate with uiSrefActive[Equals]
@@ -583,13 +592,24 @@ uiSrefActiveDirective = ['$state', '$stateParams', '$interpolate', '$uiRouter',
583592 setStatesFromDefinitionObject ( uiSrefActive ) ;
584593 }
585594
586- function setStatesFromDefinitionObject ( statesDefinition : any ) {
595+ function setStatesFromDefinitionObject ( statesDefinition : object ) {
587596 if ( isObject ( statesDefinition ) ) {
588597 states = [ ] ;
589- forEach ( statesDefinition , function ( stateOrName : StateOrName , activeClass : string ) {
590- if ( isString ( stateOrName ) ) {
598+ forEach ( statesDefinition , function ( stateOrName : StateOrName | Array < StateOrName > , activeClass : string ) {
599+ // Helper function to abstract adding state.
600+ const addStateForClass = function ( stateOrName : string , activeClass : string ) {
591601 const ref = parseStateRef ( stateOrName ) ;
592602 addState ( ref . state , $scope . $eval ( ref . paramExpr ) , activeClass ) ;
603+ } ;
604+
605+ if ( isString ( stateOrName ) ) {
606+ // If state is string, just add it.
607+ addStateForClass ( stateOrName as string , activeClass )
608+ } else if ( isArray ( stateOrName ) ) {
609+ // If state is an array, iterate over it and add each array item individually.
610+ forEach ( stateOrName , function ( stateOrName : string ) {
611+ addStateForClass ( stateOrName , activeClass )
612+ } ) ;
593613 }
594614 } ) ;
595615 }
0 commit comments