【发布时间】:2017-03-23 06:14:44
【问题描述】:
主要是因为我是 ui-router 的新手,所以无法管理我的父子状态。但是,我进行了研究并遇到了需要对路线进行硬编码但不是动态的解决方案。
我目前拥有的:
<div> <!-- parent state -->
<div ng-if="'.' | isState">
<!--
background hierarchy that should be visible only when
parent state is active and not a child state of the parent
-->
</div>
<nav>
<a ui-sref="." ui-sref-active="active">Parent</a>
<a ui-sref=".child1" ui-sref-active="active">Child 1</a>
<a ui-sref=".child2" ui-sref-active="active">Child 2</a>
</nav>
<div ui-view></div>
</div>
我想要完成什么?
当父母加载时,我希望背景显示一些东西。它占据了整个屏幕。 ui-view 不会占据整个屏幕。因此,我希望在路由与父级状态不完全匹配时隐藏或删除背景元素(如果父级路由是/root/parent,我希望背景仅在状态恰好为/root/parent 而不是/root/parent/child1 时可见) .
只有当我提供准确的预期路线时,我才能实现我想要的。例如ng-if="'root.parent' | isState"。
此外,ui-sref-active="active" 在显示子视图时保留 active 类(这是预期的,但我希望该类仅在路线完全匹配时才处于活动状态)。
但是,我认为将父级的路由硬编码到视图中并不好。我只是希望它匹配它所属的任何路线。因此,如果我稍后决定将其移至 /root/parent/car,则不必更新 ng-if 和 ui-sref-active 以反映更改。
有没有人遇到过这个问题并且能够解决它?
【问题讨论】:
-
你有父子层次结构的原因是什么?
-
从子路由
<a ui-sref=".child1">Child 1</a>中删除active类,并将css添加到active类中,使其仅出现在背景中。 -
@arqam 页面的主要内容实际上显示为背景,子项包含可以隐藏的可选内容。
标签: angularjs angular-ui-router parent-child nested-routes