使用Angular-ui-router
,是否可以定义具有多个父状态的子状态:
$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;
}
})
示例:
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.
为每个排列定义状态(例如 state1.child、state2.child 等)似乎并不正确。需要有更好的方法。
最佳答案
这种层次结构会违背 DOM 树结构,根据定义,DOM 树结构不允许同一元素有多个父元素。
此外,它很容易出错(并且令人头痛),并且很容易导致 multiple inheritance diamond problem ,作为子状态do inherit在某些情况下来自父状态。
这听起来像是一个指令,而不是一个状态,将是您正在寻找的更好的解决方案。
编辑:
刚刚看到有一个closed issue在此,由于他到达了same conclusion,所以该页面已关闭(指令是更好的方法)
关于angularjs - 具有多个父状态的 Angular-ui-router 子状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34386009/