我认为这是可能的,但我可能是错的。从儿童状态,我可以在两个兄弟国家之间分享父母国家的“自定义数据”吗?在兄弟状态之间共享ui路由器'自定义数据'
更新:是的,这是可能的,必须有我自己的代码有问题。这是一个simplified example on plunker。由于历史原因,我的问题的其余部分继续下面。请注意,我的原始代码示例有一个错字chidl2
而不是child2
,这就是为什么@originof在他的答案中使用了它。
这是我正在尝试做的简化示例。
app.config(function ($stateProvider) {
$stateProvider
.state('parent', {
abstract: true,
template: '<div ui-view></div>'
data: {
sharedArray: []
}
})
.state('parent.child1', {
url: '/child1',
controller: 'Child1Ctrl',
templateUrl: 'js/lib/parent/child1.tpl.html',
})
.state('parent.child2', {
url: '/child2/:id',
controller: 'Child2Ctrl',
templateUrl: 'js/lib/records/parentitem.tpl.html'
});
});
app.controller('Child1Ctrl', function ($state) {
// This is always loaded first
$state.current.data.sharedArray.push("child 1 adding data data");
});
app.controller('Child2Ctrl', function ($state) {
// This is always loaded from a Child1Ctrl
console.log($state.current.data.sharedArray); // Says '[]'
})
基本上,我想:
- 设置在父阵列,抽象状态(
parent
); - 转换到子状态(
parent.child1
)并更新数组; - 转换到第二个子状态(
parent.child2
)并仍然可以访问更新数组。
目前,我可以查看和更改内部parent.child1
阵列,但是当我立即转换到parent.child2
,自定义数据,$state.current.data.sharedArray
,是空的。
我的设置有些不对,即使作为孩子的孩子 - 和嵌套的意见,所以我可以看到原始视图 - 我仍然看到一个空的'sharedArray'。在过渡之前,它已经完全填充,并且在过渡之后它是空的。我会看看我能不能在PLUNK上做出一些事情。 – Andrew
是的。我的实施必须有问题。这里是一个[working plunker sample](http://plnkr.co/edit/fy61hGJILvYYu84urokK) – Andrew