2014-03-01 82 views
-1

這是作品:在App initilisation火力地堡認證

console.log('User ID: ' + user.id + ', Provider: ' + user.provider); 

,但是這個人是不是:

$scope.authenticated.currentUser = user.id; 

我的目標是採取採取一些認證變量(電子郵件+用戶名)的然後使用它們訪問Firebase上的配置文件節點。在初始化時,我需要用戶名,電子郵件和其他一些我需要的應用程序。

crossfitApp.controller('globalIdCtrl', ["$scope",'defautProfileData','$q', function ($scope,defautProfileData,$q) { 

var dataRef = new Firebase("https://glowing-fire-5401.firebaseIO.com"); 

$scope.authenticated={ 
         currentUser: $scope.authemail, 
         emailAddress: "", 
         settings: "", 
        }; 

var chatRef = new Firebase('https://<YOUR-FIREBASE>.firebaseio.com'); 
var auth = new FirebaseSimpleLogin(chatRef, function(error, user) { 
    if (error) { 
    // an error occurred while attempting login 
    switch(error.code) { 
     case 'INVALID_EMAIL': 
     case 'INVALID_PASSWORD': 
     default: 
    } 
    } else if (user) { 
    // user authenticated with Firebase 
    console.log('User ID: ' + user.id + ', Provider: ' + user.provider); 

     $scope.authenticated.currentUser = user.id ;// 

    } else { 
    // user is logged out 
    } 
}); 

}]); //GlobaldCtrl 
+0

什麼問題:

如果情況確實如此,您都可以向角,它需要$運行使用$超時申請更正此問題?你需要提供一個更清楚解釋什麼是錯的。您的帖子開始於「這不起作用......但這不是。」那麼,這兩個都不行?不工作如何? –

+0

「這個」是指後面的函數,同樣「但這個不是:」代表下一個代碼塊 –

+0

問題不在於你的意思是「這個」。這就是說,你說某件事不起作用,然後按照「但這不是,」這意味着沒有工作。你不會說*如何*不工作,所以不可能告訴你要解決什麼問題。 –

回答

1

很可能,您遇到了Angular's HTML Compiler的問題。當你使用像ng-click/ng-submit/etc這樣的事件時,Angular觸發$ scope。$ apply(),它檢查你的$ scope變量的任何變化並將它們應用到DOM。

由於FirebaseSimpleLogin不屬於Angular的權限,它不知道當回調被觸發時,你已經更新了$ scope.authenticated.currentUser。這也可以解釋爲什麼當你調用auth.login()時它會起作用,因爲你可能通過某處的ng-click事件來調用它,這會觸發摘要檢查並發現更改。

crossfitApp.controller('globalIdCtrl', ["$scope",'defautProfileData','$q', '$timeout', function ($scope,defautProfileData,$q, $timeout) { 

/* ... */ 

var auth = new FirebaseSimpleLogin(chatRef, function(error, user) { 
    if (error) { 
    /* ... */ 
    } else if (user) { 
    $timeout(function() { 
     $scope.authenticated.currentUser = user.id ;// 
    }); 
    } else { 
    // user is logged out 
    } 
}); 
+0

解決!謝謝。現在工作! –