angularjs ui-router:查找当前状态的级别

Jef*_*rey 2 regex angularjs angularjs-directive angularjs-scope angular-ui-router

所以我使用ui-router和stateparams来嵌套子状态,它运行良好.我现在正试图找到一种方法来决定css类应用程序的状态级别.main.route1.section1将是3个级别.

这里有一些非工作代码可以帮助显示:

<div ng-class="{findState().currentCount}"></div>

app.run(function($rootScope, $state, $stateParams) {
  $rootScope.$state = $state;
  $rootScope.$stateParams = $stateParams;
  $rootScope.findState = function() {
    var currentName = $state.current.name;
    var currentMatch = currentName.match(/./g);
    $rootScope.currentCount = currentMatch.length;
  };
});
Run Code Online (Sandbox Code Playgroud)

我基本上正在寻找一种方法来取出$state.current.name等于main.route1.section1并将其分成点并计算数组中有多少并将该数字返回给mg-class.除非你有更好的想法......就像一个正则表达式过滤器?

ncu*_*ery 8

看一下对象$state.$current(而不是$state.current).特别是属性path:它是一个表示当前状态的状态层次结构的数组.

所以你要找的是: $state.$current.path.length