Using Angular-ui-router
, is there a possibility to define a child-state that has multiple parent states:
使用angular -ui-路由器,是否有可能定义具有多个父状态的子状态:
$stateProvider
.state("parent1", {
url: '/',
templateUrl: 'parent1.html'
})
.state('parent2', {
url: '/parent2',
templateUrl: 'parent2.html'
})
//Make accessible from every state parent-state
.state("child", {
url: 'child',
//TODO parents: ['parent1', 'parent2']
onEnter: function() {
//do something;
}
})
Example:
例子:
Your Angular app has a directive that is used multiple times in different states in your app. The directive itself includes a link which redirects to a different state. Depending on where the directive is used, it should append its child-state to the current active parent-state.
你的角化应用程序有一个指令,它在你的应用程序的不同状态中被多次使用。这个指令本身包含一个链接,可以重定向到不同的状态。根据指令的使用位置,它应该将其子状态附加到当前活动的父状态。
It just doesn't seem right to define states for each permutation like state1.child, state2.child etc.. There needs to be a better way.
为每个排列定义状态,比如状态1,似乎是不正确的。孩子,state2。孩子等。需要有更好的办法。
1 个解决方案
#1
3
This kind of hierarchy would go against the DOM tree structure, which by definition doesn't allow multiple parents of same element.
这种层次结构将违背DOM树结构,根据定义,它不允许相同元素的多个父元素。
Moreover, it is error (and headache) prone and could easily result in the multiple inheritance diamond problem, as child state do inherit from parent state in some cases.
此外,它很容易出错(而且令人头痛),并且很容易导致多重继承菱形问题,因为在某些情况下,子状态确实从父状态继承。
It sounds like a directive, and not a state, would be the better solution for what you're looking for.
这听起来像是一种指令,而不是一种状态,对于你正在寻找的东西来说是更好的解决方案。
EDIT:
编辑:
Just saw that there's a closed issue on this, which is closed because he reached the same conclusion (that a directive is the better way)
刚刚看到了一个封闭的问题,它是封闭的因为他得出了同样的结论(指示是更好的方法)
#1
3
This kind of hierarchy would go against the DOM tree structure, which by definition doesn't allow multiple parents of same element.
这种层次结构将违背DOM树结构,根据定义,它不允许相同元素的多个父元素。
Moreover, it is error (and headache) prone and could easily result in the multiple inheritance diamond problem, as child state do inherit from parent state in some cases.
此外,它很容易出错(而且令人头痛),并且很容易导致多重继承菱形问题,因为在某些情况下,子状态确实从父状态继承。
It sounds like a directive, and not a state, would be the better solution for what you're looking for.
这听起来像是一种指令,而不是一种状态,对于你正在寻找的东西来说是更好的解决方案。
EDIT:
编辑:
Just saw that there's a closed issue on this, which is closed because he reached the same conclusion (that a directive is the better way)
刚刚看到了一个封闭的问题,它是封闭的因为他得出了同样的结论(指示是更好的方法)