Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to uiSref target state are not propagated to other directives #140

Closed
jonrimmer opened this issue Jul 12, 2017 · 1 comment
Closed

Comments

@jonrimmer
Copy link
Contributor

If I have a dynamic binding to uiSref, as follows:

<a
  [uiSref]="someDynamicLink"
  uiSrefActive="active"
>Some Dynamic Link
</a>

Changes to someDynamicLink do not result in directives like uiSrefActive updating themselves. E.g. if the value of someDynamicLink is changed, the active state of the link is not updated to reflect whether the new value matches the current path.

The problem seems to lie with the strange way the uiSref directive defines its input properties. There is an @input('uiSref') decorator applied to the state property, but changes to this field do not cause any event to be emitted on the targetState$ subject. There is a setter called "uiSref" and marked @internalapi, which wraps state and does emit an event on targetState$, but this will not be called by Angular, as it is not marked as an input.

I was able to get things working by replacing:

@Input('uiSref') state: string;

With...

private _state: string;

/**
 * `@Input('uiSref')` The name of the state to link to
 *
 * ```html
 * <a uiSref="hoome">Home</a>
 * ```
 */
@Input('uiSref') 
set state(val) {
  this._state = val;
  this.update();
}

get state() {
  return this._state;
}

But I'm not clear on what purpose the "uiSref" property setter serves, and whether there was any rationale behind it firing updates rather than state?

@jonrimmer
Copy link
Contributor Author

jonrimmer commented Jul 12, 2017

If you need a workaround to this issue, then declaring the following directive in your own app module will makes things work, until it can be fixed upstream.

import { Directive, Input, ContentChild } from '@angular/core';
import { UISref } from '@uirouter/angular';

@Directive({
  selector: '[uiSref]'
})
export class UISrefFix {
  @ContentChild(UISref) sref: UISref;

  @Input('uiSref')
  set uiSref(val) {
    if (this.sref) {
      setTimeout(() => this.sref.update(), 0);
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant