Skip to content

Using ui-sref-active[-eq] with ng-repeat causes a race condition #1997

Closed
@joshuahiggins

Description

@joshuahiggins

As the title states, when using ui-sref-active or ui-sref-active-eq on a dynamically generated element, such as one created via ng-repeat, the element is not always ready for classes to be applied when $stateChangeSuccess is fired. This causes the classes to not apply on the initial state load, but they are applied on subsequent state changes.

Sample markup:

<ul>
  <li ng-repeat="nav in vm.navItems" ui-sref-active-eq="current" class="{{nav.classes}}">
    <a ui-sref="{{nav.state}}">
      <span ng-bind="nav.text"></span>
    </a>
  </li>
</ul>

Although UI Router matches the state properly, the update() fails to apply the classes as $element is not yet available on the DOM.

In the above code, removing the {{nav.classes}} resolves the issue, which leads me to believe it's simply a race condition between the DOM and UI Router. Wrapping the $element.addClass(activeClass) in a $timeout resolves the issue, though it causes numerous unit tests to fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions