From f0ddbe7b4a91daf279c3b7d0cee732bb1f3be5b4 Mon Sep 17 00:00:00 2001 From: Kasper Lewau Date: Tue, 14 Apr 2015 22:32:00 +0200 Subject: [PATCH] fix(uiSrefActive): Apply active classes on lazy loaded states --- src/stateDirectives.js | 14 ++++++------- test/stateDirectivesSpec.js | 39 ++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/stateDirectives.js b/src/stateDirectives.js index 8baad48c5..09991030c 100644 --- a/src/stateDirectives.js +++ b/src/stateDirectives.js @@ -239,13 +239,13 @@ function $StateRefActiveDirective($state, $stateParams, $interpolate) { // Allow uiSref to communicate with uiSrefActive[Equals] this.$$addStateInfo = function (newState, newParams) { var state = $state.get(newState, stateContext($element)); - if (state) { - states.push({ - state: state, - params: newParams - }); - update(); - } + + states.push({ + state: state || { name: newState }, + params: newParams + }); + + update(); }; $scope.$on('$stateChangeSuccess', update); diff --git a/test/stateDirectivesSpec.js b/test/stateDirectivesSpec.js index 90bed508b..c9b621b7d 100644 --- a/test/stateDirectivesSpec.js +++ b/test/stateDirectivesSpec.js @@ -392,11 +392,12 @@ describe('uiStateRef', function() { }); describe('uiSrefActive', function() { - var el, template, scope, document; + var el, template, scope, document, _stateProvider; beforeEach(module('ui.router')); beforeEach(module(function($stateProvider) { + _stateProvider = $stateProvider; $stateProvider.state('top', { url: '' }).state('contacts', { @@ -511,6 +512,42 @@ describe('uiSrefActive', function() { $q.flush(); expect(angular.element(template[0]).attr('class')).toBe('ng-scope active'); })); + + it('should match fuzzy on lazy loaded states', inject(function($rootScope, $q, $compile, $state) { + el = angular.element('
Lazy Contact
'); + template = $compile(el)($rootScope); + $rootScope.$digest(); + + $rootScope.$on('$stateNotFound', function () { + _stateProvider.state('contacts.lazy', {}); + }); + + $state.transitionTo('contacts.item', { id: 1 }); + $q.flush(); + expect(angular.element(template[0].querySelector('a')).attr('class')).toBe(''); + + $state.transitionTo('contacts.lazy'); + $q.flush(); + expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active'); + })); + + it('should match exactly on lazy loaded states', inject(function($rootScope, $q, $compile, $state) { + el = angular.element('
Lazy Contact
'); + template = $compile(el)($rootScope); + $rootScope.$digest(); + + $rootScope.$on('$stateNotFound', function () { + _stateProvider.state('contacts.lazy', {}); + }); + + $state.transitionTo('contacts.item', { id: 1 }); + $q.flush(); + expect(angular.element(template[0].querySelector('a')).attr('class')).toBe(''); + + $state.transitionTo('contacts.lazy'); + $q.flush(); + expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active'); + })); }); describe('uiView controllers or onEnter handlers', function() {