从ui路由器$ stateChangeStart获取父状态 [英] get parent state from ui-router $stateChangeStart

查看:65
本文介绍了从ui路由器$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆