角JS决心下决心(Angular JS resolve in a resolve)

2019-10-23 02:14发布

我的意思是在一个决心决心是,我希望能够先解决用户,然后解决他所有的额外信息,例如,addressess,联系人,朋友。

我目前有:

.state('website', {
            url : '',
            abstract : true,
            resolve : {
                // Request the current signed in user >
                current_user : ['Auth', function(Auth){
                    return Auth.requestUser();
                }]
            },
            views : {
                'main' : {
                    templateUrl : 'template.html'
                }
            }
        })

我会喜欢做的是以下几点:

.state('website', {
            url : '',
            abstract : true,
            resolve : {
                // Request the current signed in user >
                current_user : ['Auth', function(Auth){
                    Auth.requestUser().then(function(){
                       // HERE I WANT TO RESOLVE OTHER RESOURCES
                       // WHAT WILL I RETURN HERE?
                    });
                }]
            },
            views : {
                'main' : {
                    templateUrl : 'template.html'
                }
            }
        })

我不知道这是可能的。

Answer 1:

你可以注入“已解决”参数到其他的决心功能:

resolve: {
  current_user: ['Auth', function(Auth){
    return Auth.requestUser();
  }],

  // "Svc" here is some service I assume you have to get friends, address, etc...
  friends: ["Svc", "current_user", function(Svc, current_user){
     // Svc.getFriends can return a promise or a value
     return Svc.getFriends(current_user);
  }],

  address: ["Svc", "current_user", function(Svc, current_user){
     return Svc.getAddress(current_user);
  }]
}

然后,在控制器的这种状态下,你可以注入current_userfriendsaddress作为参数:

.controller("WebsiteMainViewCtrl", function($scope, current_user, friends, address){
   // ...
})


Answer 2:

你可以做这样的事情。

 resolve : {
       // Request the current signed in user >
       current_user : ['Auth', function(Auth){
          var deferred = $q.defer();
          Auth.requestUser().then(function(){
              // Call this deferred.resolve in inner asyc callback
              deferred.resolve(data);
          });
          return deferred.promise;

       }]
 }

要么

您还可以使用链接

  resolve : {
       // Request the current signed in user >
       current_user : ['Auth', function(Auth, userInformation){
          var authData;
          return Auth
              .requestUser()
              .then(function (_authData) {
                  // do something with dataFromAuth
                  authData = _authData;
                  return userInformation.getDetailInfoAboutUser(_authData.userid); 
              })
              .then(function (detailedUserInfoObject) {
                  // do something with dataFromOtherResource and return
                  detailedUserInfoObject.authInfo = authData;
                  return detailedUserInfoObject;
              });
       }]
 }


文章来源: Angular JS resolve in a resolve