angularjs ui-router: Get list of child states of a given state

David Lay

Do you know a way in ui-router to get the list of child states of a given state?

I'm implementing a complex SPA with maybe 4 level deep navigation using ui-router.

I would like to implement a navigation tab interface for the second level generated automatically from the router table.

For that, I need to get the list of direct children of a given state (maybe is an abstract state). The only way that I've found to get the states is to do a $state.get() and obtain the full list of states, but that means that I'll have to parse child states on my own, and I think that's code already written in ui-router.

I let this question here in case there's anyone with experience in the source code or know of a plugin or module that can help me on this. Meanwhile I will do a research on my own and post results if I find something.

David Lay

In the end, had to implement the functionality myself.

In the controller of the abstract route that has the tab interface, filtered the states using a regular expression taking advantage of the default naming convention for routes:

var routeName='Root.Parent'; // this is the "actual" route

// this regex is going to filter only direct childs of this route.
var secondRouteOnlyRegex = new RegExp(routeName + "\.[a-z]+$", "i");
var states = $state.get();

// this uses lodash to filter the states based on the regex
var navLinks = _.filter(states, (s) => {
    return secondRouteOnlyRegex.test(s.name) && !s.abstract;
});
$scope.tabs = navlinks;

My route definitions have a data property that contains the title and other metadata needed to render the tabs.

My template looks something like this (using bootstrap):

<ul class="nav nav-tabs">
    <li ng-repeat="state in tabs" ui-sref-active="active">
        <a href="{{state.url}}" ui-sref="{{state.name}}">{{state.data.title}}</a>
    </li>
</ul>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get Angularjs UI Router list of states?

From Dev

AngularJS UI-Router : Abstract state + child states not working

From Dev

Is there any way to get a list of child states in angular-ui-router?

From Dev

Angular UI Router Nested State resolve in child states

From Dev

Angular-ui-router child state with multiple parent states

From Dev

Ui-router parent state, child states controllers

From Dev

AngularJS ui-router default state for states without a URL

From Dev

UI Router - Set parent to active for all child states when ui-sref is given as link to one child

From Dev

AngularJS UI Router $state reload child state only

From Dev

angularjs ui-router default child state and ui-sref

From Dev

AngularJS Inserting child state parameter into $urlRouterProvider (UI-Router)

From Dev

Reusable child states with ui-router

From Dev

Prevent hitting child states ui-router

From Dev

Reusable child states with ui-router

From Dev

UI Router Child States in Angular not Loading Controllers

From Dev

How to get a list of states which come before a given state with Ruby `state_machine` gem?

From Dev

How to get a list of states which come before a given state with Ruby `state_machine` gem?

From Dev

angularjs - ui.router sibling states

From Dev

AngularJS, ui-router, nested states

From Dev

AngularJS UI Router shared nested routes/states

From Dev

angularjs - ui.router sibling states

From Dev

Angularjs ui.router chid states not loading

From Dev

AngularJS UI-Router : Get the absolute URL of the state with the parameters

From Dev

AngularJS UI-Router - Cannot get a nested state to show the template

From Dev

AngularJS UI-Router get the current state from within a view

From Dev

What is the minimum needed to preserve angularjs ui-router states with sticky states from ui-router-extras?

From Dev

AngularJS UI-Router: Two way binding between resolved object in child/parent state—Child state won't update

From Dev

Angular ui-router parallel view with child states

From Dev

How to setup a parent/child with a resolve and nested states in ui-router?

Related Related

  1. 1

    How to get Angularjs UI Router list of states?

  2. 2

    AngularJS UI-Router : Abstract state + child states not working

  3. 3

    Is there any way to get a list of child states in angular-ui-router?

  4. 4

    Angular UI Router Nested State resolve in child states

  5. 5

    Angular-ui-router child state with multiple parent states

  6. 6

    Ui-router parent state, child states controllers

  7. 7

    AngularJS ui-router default state for states without a URL

  8. 8

    UI Router - Set parent to active for all child states when ui-sref is given as link to one child

  9. 9

    AngularJS UI Router $state reload child state only

  10. 10

    angularjs ui-router default child state and ui-sref

  11. 11

    AngularJS Inserting child state parameter into $urlRouterProvider (UI-Router)

  12. 12

    Reusable child states with ui-router

  13. 13

    Prevent hitting child states ui-router

  14. 14

    Reusable child states with ui-router

  15. 15

    UI Router Child States in Angular not Loading Controllers

  16. 16

    How to get a list of states which come before a given state with Ruby `state_machine` gem?

  17. 17

    How to get a list of states which come before a given state with Ruby `state_machine` gem?

  18. 18

    angularjs - ui.router sibling states

  19. 19

    AngularJS, ui-router, nested states

  20. 20

    AngularJS UI Router shared nested routes/states

  21. 21

    angularjs - ui.router sibling states

  22. 22

    Angularjs ui.router chid states not loading

  23. 23

    AngularJS UI-Router : Get the absolute URL of the state with the parameters

  24. 24

    AngularJS UI-Router - Cannot get a nested state to show the template

  25. 25

    AngularJS UI-Router get the current state from within a view

  26. 26

    What is the minimum needed to preserve angularjs ui-router states with sticky states from ui-router-extras?

  27. 27

    AngularJS UI-Router: Two way binding between resolved object in child/parent state—Child state won't update

  28. 28

    Angular ui-router parallel view with child states

  29. 29

    How to setup a parent/child with a resolve and nested states in ui-router?

HotTag

Archive