问题描述
考虑从ui路由器文档中获取的以下状态:
Considering the following states taken from the ui-router documentation:
.state('state1', {
url: '/state1',
templateUrl: 'partials/state1.html'
controller: 'State1Ctrl'
})
.state('state1.list', {
url: '/list',
templateUrl: 'partials/state1.list.html',
})
以及状态"state1"的"partials/state1.html"控制器:
And the controller for "partials/state1.html" for state "state1":
.controller('State1Ctrl', function () {
});
是否有任何内置功能可以从控制器内部或模板内部确定控制器/模板与哪个状态关联?
Is there any built-in feature to determine from within the controller or within the template, what state the controller/template is associated with?
例如:
.controller('State1Ctrl', function ($state) {
console.log($state.this); // state1
});
如果没有内置解决方案,您将如何装饰" $ state或$ stateParams以包含此信息?
And if there is no built-in solution, how would you "decorate" $state or $stateParams, to contain this information?
我想出的唯一解决方案是使用$ state.get(),然后使用控制器或模板值查找状态.但是,这似乎令人难以置信.
The only solution I've come up with is to use $state.get() and then find the state with the controller or template value. This seems incredibly messy, though.
推荐答案
在任何地方都找不到此文档,因此我查看了源代码.
Couldn't find this documented anywhere, so I looked in the source code.
ui-view元素上附加了一个名为$ uiView的数据字段,其中包含视图名称和关联的状态.您可以这样获得此状态:
There is a data field named $uiView attached to the ui-view element, it contains the view name and the associated state. You can get this state like this:
elem.closest('[ui-view]').data('$uiView').state
甚至
elem.inheritedData('$uiView').state
这篇关于AngularJS UI-Router从视图中获取当前状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!