从 ui-router $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]
?
What would I have to add? to get [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-router $stateChangeStart 获取父状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!