加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – Angular UI路由器,如何从控制器获取当前状态的祖先

发布时间:2020-12-17 07:29:12 所属栏目:安全 来源:网络整理
导读:我的州等级是(从上到下): 根 帐户 account.invoices account.invoices.detail 当我在account.invoices.detail状态时,我想获得一个祖先状态列表: angular .module('app') .controller('InvoiceDetailCtrl',['$scope','$state',function ($scope,$state) { v
我的州等级是(从上到下):

>根
>帐户
> account.invoices
> account.invoices.detail

当我在account.invoices.detail状态时,我想获得一个祖先状态列表:

angular
    .module('app')
    .controller('InvoiceDetailCtrl',['$scope','$state',function ($scope,$state) {
        var current = $state.current.name;
        // Get ancestors from current state
        // Tried $state.current.parent,$state.parent,$state.parent(current)
    }]);

Angular UI路由器允许您转换到父状态(即从具有data-ui-sref =“^”的视图),因此应该可以实现这一点(跟随祖先链到root).

我需要这个来构建类似auto-breadcrumbs的功能.

编辑:由于接受的答案,最终得到了这个:

var current = $scope.$state.$current,parent = current.parent,ancestors = [current.name];

while (typeof parent != 'undefined' && parent.name.length) {
    ancestors.push(parent.name);
    parent = parent.parent;
}

如果你问为什么检查parent.name.length是因为在Angular UI中存在类似“root”状态的东西(无法获得任何文档).

看看$state.$current.includes;它似乎返回一个对象,其中的键与当前状态和父状态匹配.我相信这些都是通过$state.includes()测试的状态.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读