我有一个状态,我需要解析一个项目才能解析另一个项目:
.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对象,并且只有在我想要检索该特定用户的行程之后。
处理这种情况的最佳方法是什么?
答案 0 :(得分:1)
首先,我使用firebase website中解释的方式,然后在trip功能中使用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();
}
}
答案 1 :(得分: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;
})
}
}
}

答案 2 :(得分:0)
让我们尝试使用promise嵌套。它应该工作,
.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();
};
})
}
}
}