Angular JS resolve in a resolve

核能气质少年 提交于 2019-12-24 14:27:43

问题


What I mean by resolve in a resolve is that I want to be able to resolve the user first, and then resolve all his additional info, eg, addressess, contacts, friends.

What I currently have:

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

What I'll like to do is the following:

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

I'm not sure if this is possible.


回答1:


You can inject "resolved" parameters into other resolve functions:

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

Then, in the controller for that state, you can inject current_user, friends, and address as parameters:

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



回答2:


You can do something like this.

 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;

       }]
 }

Or

You can also use chaining

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


来源:https://stackoverflow.com/questions/30185897/angular-js-resolve-in-a-resolve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!