Angularjs异步回调返回未在$scope.$apply()下定义;
这是我的工厂代码。回调是异步的,所以我把它放在$rootScope.safeApply()下。 然后,我在控制器中调用console.log(authService.authUser),但当用户登录时,它仍然返回undefined。但若用户未登录,则会被查找,并在控制台中显示“未登录”。有什么想法吗Angularjs异步回调返回未在$scope.$apply()下定义;,angularjs,angularjs-service,Angularjs,Angularjs Service,这是我的工厂代码。回调是异步的,所以我把它放在$rootScope.safeApply()下。 然后,我在控制器中调用console.log(authService.authUser),但当用户登录时,它仍然返回undefined。但若用户未登录,则会被查找,并在控制台中显示“未登录”。有什么想法吗 myapp.factory('authService', ['$rootScope', function($rootScope) { var auth = {}; $rootScop
myapp.factory('authService', ['$rootScope', function($rootScope) {
var auth = {};
$rootScope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if (phase == '$apply' || phase == '$digest') {
if(fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};
auth.firebaseAuthClient = new FirebaseAuthClient(FIREBASEREF, function(error, user) {
$rootScope.safeApply(function() {
if (user) {
auth.authUser = user;
//auth.isLoggedIn = true;
} else if (error) {
auth.authError = error;
} else {
auth.not = 'not login';
//auth.isLoggedIn = false;
}
});
});
auth.login = function() {
this.firebaseAuthClient.login('facebook');
};
auth.logout = function() {
this.firebaseAuthClient.logout();
};
return auth;
}]);
已更新
auth.callback = function(error, user) {
if (user) {
deferred.resolve(user);
} else if (error) {
deferred.reject(error);
} else {
//deferred.reject('not login'); // there is no callback value here
}
return deferred.promise;
}
内部控制器
callback().then(function(response) {
$scope.isLoggedIn = true;
}, function(response) {
$scope.isLoggedIn = false //How can i set false here?
});
更新2
现在一切正常,我可以监控用户登录状态。但还是有问题。检查下面的代码
authService.callback().then(function(success){
$rootScope.isLoggedIn = true; //If promise return success set isLoggedIn true
}, function(fail){
**//If user not login set isLoggedIn false;
//I have problem here because i'm not able to deferred.reject below**
$rootScope.isLoggedIn = false;
})
auth.callback = function(error, user) {
$timeout(function() {
if (user) {
deferred.resolve(user);
} else if (error) {
deferred.reject(error);
} else {
//If this line is added,
//.then() will not return anything not even undefined with no error,
//No mater user logged-in or not login.
//If I comment it out, everything will work fine but how can I
//set isLoggedIn = false?
deferred.reject();
}
}, 0);
return deferred.promise;
}
将外部服务的延迟解析封装在$timeout块中,以便在解析时通知angular。这样,当您的控制器运行然后回调时,它将处于$digest循环中 将此小提琴视为概念的有效证明:
//在控制器中
authService.login()。然后(成功,错误);
//服务
myapp.factory('authService',['$q','$timeout',函数($q,$timeout){
var auth={},
推迟;
firebaseAuthClient=新的firebaseAuthClient(FIREBASEREF,afterAuth);
函数afterAuth(错误,用户){
//让我知道延迟的问题已经解决了。
$timeout(函数(){
如果(用户){
延迟。解决(用户);
}else if(错误){
延迟。拒绝(错误);
}否则{
deferred.reject();//这里没有回调值
}
}, 0);
}
auth.login=函数(){
递延=$q.defer();
firebaseAuthClient.login('facebook');
回报。承诺;
};
auth.logout=函数(){
递延=$q.defer();
firebaseAuthClient.logout();
回报。承诺;
};
返回auth;
}]);
使用承诺,而不是那些陈词滥调的东西@OlivérKovács嗨,我用$q更新了我的问题。你能再检查一下吗?谢谢,但是如果我想要afterAuth()
standalone怎么办?因为我想用它来检查用户登录状态,而不触发.login('facebook')
基本上,我想检查每个routeAfterAuth中登录的用户是否是回调。你想如何独立?用户登录后,根据需要将状态保存到服务,或从服务器获取状态。好的,让我再试一次。。。稍后再来。好的,在尝试了一些之后,返回中的承诺。login()
和。logout()
不是我想要的。我的意思是standalone实际上是从FirebaseAuthClient
中拉出回调。这是因为我可以使用authService.afterAuth().then()
来监视用户登录状态,而无需触发.login()
。现在所有的工作都找到了,但仍然存在一个问题。请检查我的更新2