javascript - 登录/注销后无法刷新 ionic 侧菜单上的数据

标签 javascript ionic-framework

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

有什么建议吗?

这是我的代码:

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

最佳答案

解决方案:

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

关于javascript - 登录/注销后无法刷新 ionic 侧菜单上的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42413968/

相关文章:

android - 错误:无法使用Android应用程序(ionic,cordova和java应用程序)启动守护进程,并显示gradle错误

javascript - 找不到 Uncaught SyntaxError : Invalid or unexpected token on an array 的原因

javascript - 在 Canvas 中沿贝塞尔曲线移动 Sprite

ionic-framework - 扫描已经在 iOS13 中的 ionic V3 问题

java - 搜索栏未在 Ionic 上显示

javascript - ng-click 指令不调用 Controller 函数

android - ionic cordova run android 在上次更新后停止工作

javascript - 反转堆叠条形图

javascript - 更改 tr 的多个类 (HTML/JQUERY)

Javascript:填充数组之间的缺失值