0

我有,我需要解决一个项目之前,我有数据的状态来解决其他:Angularjs两阶段解决(解决依赖性)

 .state('addtrip', { 
     url: '/addtrip', 
     templateUrl: 'views/addtrip.html', 
     controller: 'AddtripCtrl', 
     resolve: { 
      auth : function($state, Auth){ 
      return Auth.$requireAuth().then(function(auth) { 
       return Auth; 
      }, function(error){ 
       $state.go('login'); 
      }); 
      }, 
      trips : function(rootRef,$firebaseArray){ 
      return $firebaseArray(rootRef).child(auth.uid).$loaded(); 
      } 
     } 

所以首先我想给auth对象,只在我想要检索特定用户的行程之后。

处理这种情况的最佳方法是什么?

回答

1

上面的人忘了在内部功能

.state('addtrip', { 
 
     url: '/addtrip', 
 
     templateUrl: 'views/addtrip.html', 
 
     controller: 'AddtripCtrl', 
 
     resolve: { 
 
      auth :function($state, Auth,rootRef,$firebaseArray,$q){ 
 
      return Auth.$requireAuth().then(function(auth) { 
 
       var p = new Promise (resolve, reject) 
 
       resolve({ 
 
       auth: auth, 
 
       trips: $firebaseArray(rootRef).child(auth.uid).$loaded(); 
 
       }); 
 
      return p; 
 
      }) 
 
     } 
 

 
    } 
 
}

0

比让我们尝试与诺言嵌套。它应该工作,

.state('addtrip', { 
    url: '/addtrip', 
    templateUrl: 'views/addtrip.html', 
    controller: 'AddtripCtrl', 
    resolve: { 
     auth :function($state, Auth,rootRef,$firebaseArray,$q){ 
      var defer = $q.defer(); 
      var obj = []; 
      Auth.$requireAuth().then(function(auth) { 
      obj.push(auth); 
      var a = $firebaseArray(rootRef).child(auth.uid).$loaded(); 
      obj.push(a); 
      defer.resolve(obj); 
     }); 
     return defer.promise; 
     } 
} 

OR,所有的

.state('addtrip', { 
     url: '/addtrip', 
     templateUrl: 'views/addtrip.html', 
     controller: 'AddtripCtrl', 
     resolve: { 
      auth :function($state, Auth,rootRef,$firebaseArray,$q){ 
      return Auth.$requireAuth().then(function(auth) { 
    return { 
      auth: auth, 
      trips: $firebaseArray(rootRef).child(auth.uid).$loaded(); 
      }; 
      }) 
     } 

} 
     } 
+0

它不工作回报的承诺。他只解决auth,但是当我添加旅行解决它的失败。 – user4550050

+0

你能发布错误吗? – Ved

+0

没有错误,但页面没有上传。 – user4550050

1

首先我使用的firebase website的方式解释,然后只用currentauth和waitforauth在您的旅行功能。你将不得不改变它适合你的程序,但我自己使用它,它的工作原理是这样的。 (对不起,代码缩进不好)

.run(["$rootScope", "$state", function($rootScope, $state) { 
    $rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) { 
     // We can catch the error thrown when the $requireAuth promise is rejected 
     // and redirect the user back to the home page 
     if (error === "AUTH_REQUIRED") { 
      $state.go("login"); 
     } 
    }); 
}]) 


    .state('addtrip', { 
    url: '/addtrip', 
    templateUrl: 'views/addtrip.html', 
    controller: 'AddtripCtrl', 
    resolve: { 
     "currentAuth": ["firebaseRef", function (firebaseRef) { 
         // $requireAuth returns a promise so the resolve waits for it to complete 
         // If the promise is rejected, it will throw a $stateChangeError (see above) 
         //return firebaseRef.refAuth().$requireAuth(); 
         return firebaseRef.refAuth().$requireSignIn(); 
        }], 
        "waitForAuth": ["firebaseRef", function (firebaseRef) { 
         // $requireAuth returns a promise so the resolve waits for it to complete 
         // If the promise is rejected, it will throw a $stateChangeError (see above) 
         return firebaseRef.refAuth().$waitForSignIn(); 
        }], 
     trips : function(currentAuth, waitForAuth, rootRef,$firebaseArray){ 
     return $firebaseArray(rootRef).child(currentAuth.uid).$loaded(); 
     } 
    }