UI路由器 - 检查状态存在

问题描述:

我想是这样的:UI路由器 - 检查状态存在

if ($state.get("^")) $state.go("^"); else $location.path('/');

但它显然是不行的(它总是与$state.go去,即使没有父母的状态)。如何正确检查某个特定状态是否存在?

你可以使用$ state.href,如果状态不存在,它将返回null。请参阅http://angular-ui.github.io/ui-router/site/#/api/ui.router.state。$ state

+0

谢谢!所以这应该按预期工作:'if($ state.href(“^”))$ state.go(“^”);其他$ location.path('/');'? – MikiSoft