Skip to content

Commit 9698ec4

Browse files
fix(ui-sref): update ui-sref-active/eq info when params change
When ui-state dynamicly changes watchers, make sure to update the ui-sref-active/eq Clear out old previous registered values. Closes #2554
1 parent abb3deb commit 9698ec4

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/stateDirectives.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,16 @@ function $StateRefDirective($state, $timeout) {
123123
var def = { state: ref.state, href: null, params: null };
124124
var type = getTypeInfo(element);
125125
var active = uiSrefActive[1] || uiSrefActive[0];
126+
var unlinkInfoFn = null;
126127

127128
def.options = extend(defaultOpts(element, $state), attrs.uiSrefOpts ? scope.$eval(attrs.uiSrefOpts) : {});
128129

129130
var update = function(val) {
130131
if (val) def.params = angular.copy(val);
131132
def.href = $state.href(ref.state, def.params, def.options);
132133

133-
if (active) active.$$addStateInfo(ref.state, def.params);
134+
if (unlinkInfoFn) unlinkInfoFn();
135+
if (active) unlinkInfoFn = active.$$addStateInfo(ref.state, def.params);
134136
if (def.href !== null) attrs.$set(type.attr, def.href);
135137
};
136138

@@ -179,10 +181,7 @@ function $StateRefDynamicDirective($state, $timeout) {
179181
def.state = group[0]; def.params = group[1]; def.options = group[2];
180182
def.href = $state.href(def.state, def.params, def.options);
181183

182-
if (unlinkInfoFn) {
183-
unlinkInfoFn();
184-
unlinkInfoFn = null;
185-
}
184+
if (unlinkInfoFn) unlinkInfoFn();
186185
if (active) unlinkInfoFn = active.$$addStateInfo(def.state, def.params);
187186
if (def.href) attrs.$set(type.attr, def.href);
188187
}

test/stateDirectivesSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,24 @@ describe('uiSrefActive', function() {
539539
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
540540
}));
541541

542+
it('should update in response to ui-sref param expression changes', inject(function($rootScope, $q, $compile, $state) {
543+
el = angular.element('<div><a ui-sref="contacts.item.detail({ foo: fooId })" ui-sref-active="active">Contacts</a></div>');
544+
template = $compile(el)($rootScope);
545+
$rootScope.fooId = 'bar'
546+
$rootScope.$digest();
547+
548+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
549+
$state.transitionTo('contacts.item.detail', { id: 5, foo: 'bar' });
550+
$q.flush();
551+
timeoutFlush();
552+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
553+
554+
$rootScope.fooId = 'baz'
555+
$q.flush();
556+
timeoutFlush();
557+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
558+
}));
559+
542560
it('should match on child states', inject(function($rootScope, $q, $compile, $state) {
543561
template = $compile('<div><a ui-sref="contacts.item({ id: 1 })" ui-sref-active="active">Contacts</a></div>')($rootScope);
544562
$rootScope.$digest();

0 commit comments

Comments
 (0)