2017-02-23 58 views
0

使用離子創建器時,登錄/註銷時出現問題,我的側邊菜單信息(頭像,用戶名...)保留上一個用戶的信息(I在兩個帳戶之間切換以進行測試)。我不知道如何刷新或刪除與前一個用戶相關的所有信息,以便爲下一個用戶刷新我的側邊菜單。我可以在控制檯中看到加載了正確的值,但它們不會擦除菜單中的前一個值。登錄/註銷後無法刷新離子菜單上的數據

有什麼建議嗎?

這裏是我的代碼:

// SIGNOUT FUNCTION in the controller 
 

 
$scope.signOut = function(){ 
 
    firebase.auth().signOut().then(function() { 
 
      
 
      // This is to try to "clean" "clear" all informations of the previous user 
 
      $ionicLoading.show({ 
 
      template: 'Logging out....' 
 
      }); 
 
    
 
       $timeout(function() { 
 
       $ionicLoading.hide(); 
 
       $ionicHistory.clearCache(); 
 
       $ionicHistory.clearHistory(); 
 
       $ionicHistory.nextViewOptions({ 
 
       disableBack: true, 
 
       historyRoot: true 
 
       }); 
 
      $state.go("connexion"); 
 
     }, 30);   
 
    }); 
 
}; 
 

 
// MAJ DOM (menu) FUNCTION in the controller 
 
// function used for refresh the menu informations (avatar, displayname...) 
 

 
$scope.majDom = function() 
 
{ 
 
    //$state.reload(); 
 
    var user = firebase.auth().currentUser; 
 
    var uid; 
 
    $scope.userData = user; 
 

 
    if (user !== null) { 
 
     $scope.userData.displayName = user.displayName; 
 
     $scope.userData.email = user.email; 
 
     $scope.userData.avatar = user.photoURL; 
 
     uid = user.uid; 
 
     //With these lines, I can read in the console that the right informations are loaded after a new login, but never appear in the menu, instead there are previous user's informations. 
 
     console.log("Avatar de l'user :", $scope.userData.avatar); 
 
     console.log("Nom de l'user :", $scope.userData.displayName); 
 
     console.log("Email de l'user :", $scope.userData.email); 
 
    } 
 
} 
 

 
// LOGIN FUNCTION in the controller 
 

 
$scope.loginEmail = function($email, $password){ 
 
    
 
    var alertPopup; 
 
    
 
    function signInSuccess(response) { 
 
     console.log('signInSuccess : ', response); 
 
     // This is to call a menu refresh, but dosen't work 
 
     $scope.majDom(); 
 
     $state.go("menu.VNements"); 
 
    } 
 
    
 
firebase.auth().signInWithEmailAndPassword($email, $password) 
 
    .then(signInSuccess) 
 
}; 
 

 

 
// MENU FUNCTION in the Menu Page of the creator 
 

 
function ($scope, $stateParams, $state, $firebaseArray, $ionicHistory) { 
 
    
 
//$ionicUser, $ionicAuth, 
 

 
    var user = firebase.auth().currentUser; 
 
    var uid; 
 
    $scope.userData = user; 
 
    
 
    $ionicHistory.clearCache(); 
 
    $ionicHistory.clearHistory(); 
 
    $ionicHistory.nextViewOptions({ 
 
     disableBack: true, 
 
     historyRoot: true 
 
    }); 
 
     
 
    if (user !== null) { 
 
     $scope.userData.displayName = user.displayName; 
 
     $scope.userData.email = user.email; 
 
     $scope.userData.avatar = user.photoURL; 
 
     uid = user.uid; 
 
     console.log("Avatar de l'user :", $scope.userData.avatar); 
 
     console.log("Nom de l'user :", $scope.userData.displayName); 
 
     console.log("Email de l'user :", $scope.userData.email); 
 
    }
In the side menu's HTML the Avatar is called like this : 
 

 
{{userData.avatar}} 
 

 

 
And the display name : 
 

 
{{userData.displayName}}

+0

請,是任何人有一個想法? – Memphis

回答

1

解決方案

我想通了這一點:),該解決方案是初始化$範圍內的當前用戶。 $開,因爲問題是當你創建一個離子側面菜單的應用程序,菜單創建一次與你給他的信息,刷新它,你需要有一個事件功能就像$ scope。$ on一樣。在這種特殊情況下,只需在您的HTML中使用{{data.value}}即可。

// MAJ DOM (menu) FUNCTION in the controller 
 
// function used for refresh the menu informations (avatar, displayname...) 
 

 
$scope.majDom = function() 
 
{  
 
    var user; 
 
    var uid; 
 
    
 
    $scope.$on('$ionicView.enter', function() { 
 
      
 
     user = firebase.auth().currentUser; 
 
     $scope.userData = user; 
 
    
 
     $scope.userData.displayName = user.displayName; 
 
     $scope.userData.email = user.email; 
 
     $scope.userData.avatar = user.photoURL; 
 
     uid = user.uid; 
 
     console.log("MENU : Avatar de l'user :", $scope.userData.avatar); 
 
     console.log("MENU : Nom de l'user :", $scope.userData.displayName); 
 
     console.log("MENU : Email de l'user :", $scope.userData.email); 
 
    }); 
 
}