angularjs - 刷新子状态视图原因丢失的数据从父状态状态、视图、原因、数据

2023-09-14 00:08:30 作者:青鸾

我有以下的用户界面路由器的状态。

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'
})

我必须使用HTTP资源在userViewController至$ 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 国家申报的决心财产。由于您使用嵌套议决在这里你可以再注入该解决数据到 admin.userView.home 状态声明。下面是从UI路由器文件的摘录。

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;
      }

来源