Skip to content

Commit

Permalink
fix(ui-sref): update ui-sref-active/eq info when params change
Browse files Browse the repository at this point in the history
When ui-state dynamicly changes watchers, make sure to update the ui-sref-active/eq
Clear out old previous registered values.
Closes #2554
  • Loading branch information
christopherthielen committed Feb 14, 2016
1 parent 025ebc8 commit dcbaebf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/ng1/stateDirectives.ts
Expand Up @@ -129,14 +129,16 @@ function $StateRefDirective($state, $timeout) {
var def = { state: ref.state, href: null, params: null, options: null };
var type = getTypeInfo(element);
var active = uiSrefActive[1] || uiSrefActive[0];
var unlinkInfoFn = null;

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

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

if (active) active.$$addStateInfo(ref.state, def.params);
if (unlinkInfoFn) unlinkInfoFn();
if (active) unlinkInfoFn = active.$$addStateInfo(ref.state, def.params);
if (def.href !== null) attrs.$set(type.attr, def.href);
};

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

if (unlinkInfoFn) {
unlinkInfoFn();
unlinkInfoFn = null;
}
if (unlinkInfoFn) unlinkInfoFn();
if (active) unlinkInfoFn = active.$$addStateInfo(def.state, def.params);
if (def.href) attrs.$set(type.attr, def.href);
}
Expand Down
18 changes: 18 additions & 0 deletions test/stateDirectivesSpec.js
Expand Up @@ -615,6 +615,24 @@ describe('uiSrefActive', function() {
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
}));

it('should update in response to ui-sref param expression changes', inject(function($rootScope, $q, $compile, $state) {
el = angular.element('<div><a ui-sref="contacts.item.detail({ foo: fooId })" ui-sref-active="active">Contacts</a></div>');
template = $compile(el)($rootScope);
$rootScope.fooId = 'bar'
$rootScope.$digest();

expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
$state.transitionTo('contacts.item.detail', { id: 5, foo: 'bar' });
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('active');

$rootScope.fooId = 'baz'
$q.flush();
timeoutFlush();
expect(angular.element(template[0].querySelector('a')).attr('class')).toBe('');
}));

it('should match on child states', inject(function($rootScope, $q, $compile, $state) {
template = $compile('<div><a ui-sref="contacts.item({ id: 1 })" ui-sref-active="active">Contacts</a></div>')($rootScope);
$rootScope.$digest();
Expand Down

0 comments on commit dcbaebf

Please sign in to comment.