从ui路由器$ stateChangeStart获取父状态 [英] get parent state from ui-router $stateChangeStart
问题描述
检测状态更改时,我想访问父状态的名称. 到目前为止,我的代码:
I want to access the name of the parent state when detecting a statechange. My code so far:
app.run(function($rootScope, $state, authProvider) {
$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {
console.log(toState.name);
});
});
返回[parentState.childState]
.一种可能是使用正则表达式替换."之后的每个符号.但是我猜想ui-router中必须有一个函数来获取父状态的名称.
Returns [parentState.childState]
. One possibility would be to use regex to replace every symbole that comes after the "." but I guess there has to be a function in ui-router to get the name of the parent state.
我要添加什么?获得[parentState]
?
示例:
用户从状态login
进入状态pets.dogs
. pets
是抽象父状态,dogs
是它的子状态.
fromState.name
将返回login
,但我想要pets
.
Example:
The user comes from the state login
to the state pets.dogs
. pets
is an abstract parent state, dogs
is a child of it.
fromState.name
would return login
but I want pets
.
是的,可以采用toState.name.split(".")[0]
的解决方法,但只能在受控条件下使用.
Yes, the workaround toState.name.split(".")[0]
is possible but only works under controlled conditions.
推荐答案
我认为您可以做到toState.parent.name
.
这篇关于从ui路由器$ stateChangeStart获取父状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!