Angularjs 两阶段解决(解决依赖)
Angularjs two stage resolve (resolve dependencies)
我有一个状态,我需要先解决一个项目,然后才能获得解决另一个项目的数据:
.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 对象,然后才想检索该特定用户的行程。
处理这种情况的最佳方法是什么?
那么让我们尝试使用 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;
}
}
或者,
.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();
};
})
}
}
}
首先,我使用 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();
}
}
上面的人忘记了 return 对内部函数的承诺
.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;
})
}
}
}
我有一个状态,我需要先解决一个项目,然后才能获得解决另一个项目的数据:
.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 对象,然后才想检索该特定用户的行程。
处理这种情况的最佳方法是什么?
那么让我们尝试使用 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;
}
}
或者,
.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();
};
})
}
}
}
首先,我使用 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();
}
}
上面的人忘记了 return 对内部函数的承诺
.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;
})
}
}
}