Skip to content

Commit f0ddbe7

Browse files
committed
fix(uiSrefActive): Apply active classes on lazy loaded states
1 parent bc34b45 commit f0ddbe7

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/stateDirectives.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ function $StateRefActiveDirective($state, $stateParams, $interpolate) {
239239
// Allow uiSref to communicate with uiSrefActive[Equals]
240240
this.$$addStateInfo = function (newState, newParams) {
241241
var state = $state.get(newState, stateContext($element));
242-
if (state) {
243-
states.push({
244-
state: state,
245-
params: newParams
246-
});
247-
update();
248-
}
242+
243+
states.push({
244+
state: state || { name: newState },
245+
params: newParams
246+
});
247+
248+
update();
249249
};
250250

251251
$scope.$on('$stateChangeSuccess', update);

test/stateDirectivesSpec.js

+38-1
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,12 @@ describe('uiStateRef', function() {
392392
});
393393

394394
describe('uiSrefActive', function() {
395-
var el, template, scope, document;
395+
var el, template, scope, document, _stateProvider;
396396

397397
beforeEach(module('ui.router'));
398398

399399
beforeEach(module(function($stateProvider) {
400+
_stateProvider = $stateProvider;
400401
$stateProvider.state('top', {
401402
url: ''
402403
}).state('contacts', {
@@ -511,6 +512,42 @@ describe('uiSrefActive', function() {
511512
$q.flush();
512513
expect(angular.element(template[0]).attr('class')).toBe('ng-scope active');
513514
}));
515+
516+
it('should match fuzzy on lazy loaded states', inject(function($rootScope, $q, $compile, $state) {
517+
el = angular.element('<div><a ui-sref="contacts.lazy" ui-sref-active="active">Lazy Contact</a></div>');
518+
template = $compile(el)($rootScope);
519+
$rootScope.$digest();
520+
521+
$rootScope.$on('$stateNotFound', function () {
522+
_stateProvider.state('contacts.lazy', {});
523+
});
524+
525+
$state.transitionTo('contacts.item', { id: 1 });
526+
$q.flush();
527+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
528+
529+
$state.transitionTo('contacts.lazy');
530+
$q.flush();
531+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
532+
}));
533+
534+
it('should match exactly on lazy loaded states', inject(function($rootScope, $q, $compile, $state) {
535+
el = angular.element('<div><a ui-sref="contacts.lazy" ui-sref-active-eq="active">Lazy Contact</a></div>');
536+
template = $compile(el)($rootScope);
537+
$rootScope.$digest();
538+
539+
$rootScope.$on('$stateNotFound', function () {
540+
_stateProvider.state('contacts.lazy', {});
541+
});
542+
543+
$state.transitionTo('contacts.item', { id: 1 });
544+
$q.flush();
545+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
546+
547+
$state.transitionTo('contacts.lazy');
548+
$q.flush();
549+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
550+
}));
514551
});
515552

516553
describe('uiView controllers or onEnter handlers', function() {

0 commit comments

Comments
 (0)