Angular JS解决了这个问题

时间:2015-05-12 08:45:02

标签: javascript angularjs angular-ui-router

我的意思是决心解决的问题是我希望能够先解析用户,然后解决所有其他信息,例如收件人,联系人,朋友。

我现在有什么:

#include <stdio.h>

typedef struct task task;
typedef int (*executor) (task* self);

struct task {
    executor exectr;
    int a;
};

int exec(task* self) {
    return self->a;
}

int main(int, char**) {
    task a = { .exectr = exec, .a=10};
    printf("%d\n",a.exectr(&a));
    return 0;
}

我想做的是以下内容:

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

我不确定这是否可行。

2 个答案:

答案 0 :(得分: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;
              });
       }]
 }

答案 1 :(得分:2)

你可以注射&#34;已解决&#34;参数进入其他解析函数:

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){
   // ...
})