How to see what states are configured in AngularJS / UI-Router?

被刻印的时光 ゝ 提交于 2019-12-18 13:52:28

问题


Is there a way to see all of the states that have been set on $stateProvider?

In this case, I would like my state assignments to be distributed across many files. I would like to inspect the built states on run or config in a different file.

For example:

# component1.coffee
angular.module('zoo').config ($stateProvider) ->
  $stateProvider.state 'component1',
    url: '/component1'
    template: _template
    controller: 'Component1Ctrl'

# component2.coffee
angular.module('zoo').config ($stateProvider) ->
  $stateProvider.state 'component2',
    url: '/component2'
    template: _template
    controller: 'Component2Ctrl'

# componentNavigation.coffee
angular.module('zoo').run ($state) ->
  console.log 'All configured states:', $state.configuredStates # doesn't exist.

Is there something that will list out the two states, component1 and component2?


回答1:


$state.get()

returns an array of all states. Includes the top-level abstract state, but you can filter that out if you want.

How to enumerate registered states in ui-router?




回答2:


For people who try to get actual URL routes including properly displayed nested states:

$state.get().map(function(state) { return $state.href(state.name) })

// => ['/login', '/home', '/something']


来源:https://stackoverflow.com/questions/21590466/how-to-see-what-states-are-configured-in-angularjs-ui-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!