有一个名为$uiView 的数据字段附加到ui-view 元素,它包含视图名称和关联的状态。你可以得到这样的状态:
elem.closest('[ui-view]').data('$uiView').state
甚至
elem.inheritedData('$uiView').state
所以,在你的控制器中:
.controller('State1Ctrl', function ($state) {
console.log(elem.closest('[ui-view]').data('$uiView').state); // state1
console.log($state.current.name) ;//will give the state name as well.
});
更新:
Your issue:https://github.com/angular-ui/ui-router/issues/1651
解决方法:
ANGULAR-UI-ROUTER: Resolve state from URL
您可以使用$stateProvider 上的.decorator 挂钩来公开内部状态实现。您可以装饰状态生成器的任何属性;有人随意选择了'parent'。
app.config(function($stateProvider) {
$stateProvider.decorator('parent', function (internalStateObj, parentFn) {
// This fn is called by StateBuilder each time a state is registered
// The first arg is the internal state. Capture it and add an accessor to public state object.
internalStateObj.self.$$state = function() { return internalStateObj; };
// pass through to default .parent() function
return parentFn(internalStateObj);
});
});
现在您可以使用 .$$state() 访问内部状态对象,例如
var publicState = $state.get("foo");
var privateInternalState = publicState.$$state();
//Second, loop over each state in $state.get() and test them against your URL fragment.
angular.forEach($state.get(), function(state) {
var privatePortion = state.$$state();
var match = privatePortion.url.exec(url, queryParams);
if (match) console.log("Matched state: " + state.name + " and parameters: " + match);
});