登录/注销后,无法在离子侧菜单上刷新数据

时间:2017-02-23 11:05:47

标签: javascript ionic-framework

使用离子创建器,我在登录/注销时出现问题,我的侧面菜单信息(头像,用户名...)保留了之前用户的信息(我在两个帐户之间进行切换以进行测试)。我不知道如何刷新或删除与前一个用户相关的所有信息,以刷新下一个用户的侧边菜单。我可以在控制台中看到正确的值已加载,但它们不会删除菜单中的上一个值。

有什么建议吗?

这是我的代码:



// 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}}




1 个答案:

答案 0 :(得分:1)

解决方案

我想出来了:),解决方案是在$ scope。$ on中初始化当前用户,因为问题是当你创建一个离子侧菜单应用程序时,菜单会使用你给出的信息创建一次他要刷新它,你需要有一个像$ 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);
    });
}