@@ -511,6 +511,17 @@ uiStateDirective = ['$uiRouter', '$timeout',
511
511
* </div>
512
512
* ```
513
513
*
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
+ *
514
525
* When the current state is "admin.roles" the "active" class will be applied to both the `<div>` and `<a>` elements.
515
526
* It is important to note that the state names/globs passed to `ui-sref-active` override any state provided by a linked `ui-sref`.
516
527
*
@@ -543,9 +554,7 @@ uiSrefActiveDirective = ['$state', '$stateParams', '$interpolate', '$uiRouter',
543
554
// Do nothing. uiSrefActive is not a valid expression.
544
555
// Fall back to using $interpolate below
545
556
}
546
- if ( ! uiSrefActive ) {
547
- uiSrefActive = $interpolate ( $attrs . uiSrefActive || '' , false ) ( $scope ) ;
548
- }
557
+ uiSrefActive = uiSrefActive || $interpolate ( $attrs . uiSrefActive || '' , false ) ( $scope ) ;
549
558
setStatesFromDefinitionObject ( uiSrefActive ) ;
550
559
551
560
// Allow uiSref to communicate with uiSrefActive[Equals]
@@ -583,13 +592,24 @@ uiSrefActiveDirective = ['$state', '$stateParams', '$interpolate', '$uiRouter',
583
592
setStatesFromDefinitionObject ( uiSrefActive ) ;
584
593
}
585
594
586
- function setStatesFromDefinitionObject ( statesDefinition : any ) {
595
+ function setStatesFromDefinitionObject ( statesDefinition : object ) {
587
596
if ( isObject ( statesDefinition ) ) {
588
597
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 ) {
591
601
const ref = parseStateRef ( stateOrName ) ;
592
602
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
+ } ) ;
593
613
}
594
614
} ) ;
595
615
}
0 commit comments