![](/img/trans.png)
[英]Provider 'xx' must return a value from $get factory method in AngularJs
[英]Fixing AngularJS error: Provider must return value from $get factory method
當我使用下面的代碼時,出現以下錯誤:
提供程序“登錄”必須從$ get工廠方法返回一個值。
我已經檢查過此處和此處的stackoverflow帖子,但無法弄清楚我在做什么錯。
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
而言,您必須返回一個對象。 由於您不返回任何內容,這意味着其他服務/控制器無法使用此服務。
如果您只是在檢查身份驗證,則它必須在您的Auth服務中,該服務必須是IIFE函數。 它將檢查並重定向用戶。
例如:
在任一Auth / Ref服務中,創建一個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()
方法中,然后在服務中調用它。 因此,它將僅運行一次。
當您想為應用程序的其他部分公開單例接口時,必須使用service。
您必須返回一個對象或函數或從工廠獲取一個值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.