Angular JS解决了这个问题

时间:2022-11-04 19:43:46

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.

我不确定这是否可行。

2 个解决方案

#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:

然后,在该状态的控制器中,您可以将current_user,friends和address注入为参数:

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

#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:

然后,在该状态的控制器中,您可以将current_user,friends和address注入为参数:

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