angularjs ui-router:获取给定状态的子状态列表 [英] angularjs ui-router: Get list of child states of a given state

查看:84
本文介绍了angularjs ui-router:获取给定状态的子状态列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道在ui-router中获取给定状态的子状态列表的方法吗?

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

我正在使用ui-router实现可能具有4级深度导航的复杂SPA.

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.

为此,我需要获取给定状态(也许是抽象状态)的直接子级的列表.我发现获取状态的唯一方法是执行$state.get()并获取状态的完整列表,但这意味着我必须自己解析子状态,而且我认为这是已经编写的代码在ui路由器中.

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.

推荐答案

最后,必须自己实现该功能.

In the end, had to implement the functionality myself.

在具有tab接口的抽象路由的控制器中,使用正则表达式利用路由的默认命名约定对状态进行过滤:

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;

我的路线定义具有data属性,该属性包含标题和呈现选项卡所需的其他元数据.

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>

这篇关于angularjs ui-router:获取给定状态的子状态列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆