Angular ui-router,如何获得与控制器相关的状态?

时间:2022-03-04 11:24:16

Plunkr demonstrating problem:

Plunkr演示问题:

http://plnkr.co/edit/Czc5kGpCwsruUQe2EanZ?p=preview

With the states:

随州:

.state('state1', {
  url: '/state1',
  template: '<div>state1 <pre>{{current | json }}</pre><div ui-view=""></div> </div>',
  controller: 'State1Ctrl',
})
.state('state1.list', {
  url: '/list',
  template: '<div>list <pre>{{current | json }}</pre></div>',
  controller: 'State1ListCtrl',
})

And the controllers:

和控制器:

.controller('State1Ctrl', function($scope, $state, $rootScope, $controller) {
  $scope.current = $state
})

.controller('State1ListCtrl', function($scope, $state) {
  $scope.current = $state
})

Produces the result:

产生结果:

state1
{
  "url": "/list",
  "template": "<div>list <pre>{{current | json }}</pre></div>",
  "controller": "State1ListCtrl",
  "name": "state1.list"
}

list
{
  "url": "/list",
  "template": "<div>list <pre>{{current | json }}</pre></div>",
  "controller": "State1ListCtrl",
  "name": "state1.list"
}

When going directly to the state1.list state.

直接进入state1.list状态时。

I need access to the state that the controller is associated with, e.g. I want the result to be:

我需要访问控制器所关联的状态,例如我希望结果如下:

state1 (notice how this is different - it has the state1 configuration now)
{
  "url": "/state1",
  "template": "<div>list <pre>{{current | json }}</pre></div>",
  "controller": "State1Ctrl",
  "name": "state1"
}

list
{
  "url": "/list",
  "template": "<div>list <pre>{{current | json }}</pre></div>",
  "controller": "State1ListCtrl",
  "name": "state1.list"
}

I understand that state.$current is the current state, but how do you determine the state that the controller is associated with?

我理解状态。$ current是当前状态,但是如何确定控制器与之关联的状态?

1 个解决方案

#1


2  

Controllers are meant to be independent of state in ui-router, so that the same controller could be used for any state.

控制器意味着独立于ui-router中的状态,因此可以将相同的控制器用于任何状态。

But you can get any state from anywhere by using e.g. $state.get('state1.list')

但是你可以通过使用例如任何地方获得任何州$ state.get( 'state1.list')

I think you may just have to have someone somewhere know which controller is associated with which state. Whether each controller knows which state its associated with and calls $state.get('stateName') to get it, or you could build a service which will return the state for a controller name, or you could even use $state.get() to get all states and then enumerate through the list until one has a controller name that matches the controller that you are in.

我想你可能只需要某个人知道哪个控制器与哪个状态相关联。每个控制器是否知道它与哪个状态相关联并调用$ state.get('stateName')来获取它,或者你可以构建一个服务来返回控制器名称的状态,或者你甚至可以使用$ state.get( )获取所有状态,然后枚举列表,直到其中一个控制器名称与您所在的控制器相匹配。

Hope that helps.

希望有所帮助。

#1


2  

Controllers are meant to be independent of state in ui-router, so that the same controller could be used for any state.

控制器意味着独立于ui-router中的状态,因此可以将相同的控制器用于任何状态。

But you can get any state from anywhere by using e.g. $state.get('state1.list')

但是你可以通过使用例如任何地方获得任何州$ state.get( 'state1.list')

I think you may just have to have someone somewhere know which controller is associated with which state. Whether each controller knows which state its associated with and calls $state.get('stateName') to get it, or you could build a service which will return the state for a controller name, or you could even use $state.get() to get all states and then enumerate through the list until one has a controller name that matches the controller that you are in.

我想你可能只需要某个人知道哪个控制器与哪个状态相关联。每个控制器是否知道它与哪个状态相关联并调用$ state.get('stateName')来获取它,或者你可以构建一个服务来返回控制器名称的状态,或者你甚至可以使用$ state.get( )获取所有状态,然后枚举列表,直到其中一个控制器名称与您所在的控制器相匹配。

Hope that helps.

希望有所帮助。