Skip to content

Commit dcbaebf

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 025ebc8 commit dcbaebf

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/ng1/stateDirectives.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,16 @@ function $StateRefDirective($state, $timeout) {
129129
var def = { state: ref.state, href: null, params: null, options: null };
130130
var type = getTypeInfo(element);
131131
var active = uiSrefActive[1] || uiSrefActive[0];
132+
var unlinkInfoFn = null;
132133

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

135136
var update = function(val?) {
136137
if (val) def.params = angular.copy(val);
137138
def.href = $state.href(ref.state, def.params, def.options);
138139

139-
if (active) active.$$addStateInfo(ref.state, def.params);
140+
if (unlinkInfoFn) unlinkInfoFn();
141+
if (active) unlinkInfoFn = active.$$addStateInfo(ref.state, def.params);
140142
if (def.href !== null) attrs.$set(type.attr, def.href);
141143
};
142144

@@ -185,10 +187,7 @@ function $StateRefDynamicDirective($state, $timeout) {
185187
def.state = group[0]; def.params = group[1]; def.options = group[2];
186188
def.href = $state.href(def.state, def.params, def.options);
187189

188-
if (unlinkInfoFn) {
189-
unlinkInfoFn();
190-
unlinkInfoFn = null;
191-
}
190+
if (unlinkInfoFn) unlinkInfoFn();
192191
if (active) unlinkInfoFn = active.$$addStateInfo(def.state, def.params);
193192
if (def.href) attrs.$set(type.attr, def.href);
194193
}

test/stateDirectivesSpec.js

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

618+
it('should update in response to ui-sref param expression changes', inject(function($rootScope, $q, $compile, $state) {
619+
el = angular.element('<div><a ui-sref="contacts.item.detail({ foo: fooId })" ui-sref-active="active">Contacts</a></div>');
620+
template = $compile(el)($rootScope);
621+
$rootScope.fooId = 'bar'
622+
$rootScope.$digest();
623+
624+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
625+
$state.transitionTo('contacts.item.detail', { id: 5, foo: 'bar' });
626+
$q.flush();
627+
timeoutFlush();
628+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');
629+
630+
$rootScope.fooId = 'baz'
631+
$q.flush();
632+
timeoutFlush();
633+
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
634+
}));
635+
618636
it('should match on child states', inject(function($rootScope, $q, $compile, $state) {
619637
template = $compile('<div><a ui-sref="contacts.item({ id: 1 })" ui-sref-active="active">Contacts</a></div>')($rootScope);
620638
$rootScope.$digest();

0 commit comments

Comments
 (0)