修复AngularJS错误:提供程序必须从$ get工厂方法返回值 [英] Fixing AngularJS error: Provider must return value from $get factory method
问题描述
当我使用下面的代码时,出现以下错误:
When I use the code below, I am getting the following error:
提供者登录"必须从$ get工厂方法返回一个值.
Provider 'Login' must return a value from $get factory method.
我已经查看了stackoverflow帖子此处,但无法弄清楚我在做什么错.
I've reviewed the stackoverflow posts here and here, but can't figure out what I'm doing wrong.
myApp.factory('Login', function($location, $firebaseAuth, Ref, Auth) {
var authData = Ref.getAuth();
if (authData) {console.log('already logged in with ' + authData.uid)} else {
return Auth.$authAnonymously({rememberMe: true}).then(redirect, showError);
function redirect() {
$location.path('/account');
}
function showError(err) {
Login.err = err;
}
}
});
推荐答案
对于factory
,您必须返回一个对象.由于您不返回任何内容,这意味着其他服务/控制器无法使用此服务.
In terms of factory
you must return an object. Since you're not returning anything, it means that other services/controllers can't use this service.
如果仅检查身份验证,则该身份验证必须在Auth服务内部,该服务必须是IIFE函数.它将检查并重定向用户.
If you're just checking for Authentication, it must be inside your Auth service which must be an IIFE function. Which will check and redirect the user.
例如:
在任一Auth/Ref服务中,创建一个IIFE
In either Auth/Ref service, create a IIFE
(function() {
var authData = Ref.getAuth();
if (authData) {console.log('already logged in with ' + authData.uid)} else {
return Auth.$authAnonymously({rememberMe: true}).then(redirect, showError);
function redirect() {
$location.path('/account');
}
function showError(err) {
Login.err = err;
}
}
})();
否则,将代码插入init()
方法中,然后在您的服务中调用该代码.因此,它将仅运行一次.
Else insert the code inside init()
method and call that in your service. SO this will run only once.
当要向应用程序的其他部分公开单例接口时,必须使用服务.
You must use service when you want to expose a singleton interface for other parts of your application to make use of.
这篇关于修复AngularJS错误:提供程序必须从$ get工厂方法返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!