angularjs - 刷新子状态视图导致父状态数据丢失 [英] angularjs - refreshing child state view cause lose of data from the parent state
问题描述
我有以下 ui-router 状态.
I have the following ui-router states.
.state('admin.userView', {
abstract:true,
url:'/user/:id/',
templateUrl:'./views/user/view.html',
controller:'userViewController'
})
.state('admin.userView.home', {
url:'',
templateUrl:'./views/user/home.html',
controller:'userHomeController'
})
我已使用 userViewController 中的 http 资源将用户详细信息提取到 $scope.users.当我移动到 admin.userView.home 状态时,我可以访问 $scope.users .但是当我用子状态刷新页面时,$scope.users 中的值会丢失.我怎样才能得到相同的.
I have fetched user details using http resource in userViewController to $scope.users. When i move to the admin.userView.home state, I can access the $scope.users . But when i refresh page with child state, the value in the $scope.users is lost. How can i get the same.
推荐答案
尝试将该数据移动到 admin.userView
状态声明的 resolve 属性中.由于您在这里使用嵌套解析,因此您可以将解析的数据注入到 admin.userView.home
状态声明中.这是 ui-router 文档的摘录.
Try moving that data into the resolve property of the admin.userView
state declaration. Since you're using nested resolves here you can then inject that resolved data into the admin.userView.home
state declaration. Here's an excerpt from the ui-router documentation.
$stateProvider.state('parent', {
resolve:{
resA: function(){
return {'value': 'A'};
}
},
controller: function($scope, resA){
$scope.resA = resA.value;
}
})
.state('parent.child', {
resolve:{
resB: function(resA){
return {'value': resA.value + 'B'};
}
},
controller: function($scope, resA, resB){
$scope.resA2 = resA.value;
$scope.resB = resB.value;
}
这篇关于angularjs - 刷新子状态视图导致父状态数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!