简体   繁体   中英

angularjs logged in user info to be available throughout the app

I want to make the user info available to all views after login. How can I modify the code to be able to access the pseudonym from the other view?
Can you please give an example?

Here is my login controller :

app.controller("MyregisterCtrl", ["$scope", "$stateParams", "Auth", "$state", "$location", "$modal", "DatabaseRef",
    function ($scope, $stateParams, Auth, $state, $location, $modal, DatabaseRef) {
        $scope.user = {};

        $scope.signIn = function () {
            if (!$scope.user.email && !$scope.user.password) {
                toastr.error("Add email and password");
            } else {
                Auth.$signInWithEmailAndPassword($scope.user.email, $scope.user.password)
                    .then(function(firebaseUser) {
                        //=====user info=================
                        var userId = firebase.auth().currentUser.uid;
                        DatabaseRef.ref('/users/' + userId).once('value')
                            .then(function(snapshot) {
                                pseudonym = snapshot.val().pseudonym;
                                console.log("pseudonym: ", pseudonym);
                                return pseudonym;

                            });
                        //======================
                        $state.go('app.dashboard');

                        if (!firebaseUser.emailVerified) {
                            toastr.info('Your email is NOT verified.', 'Verify email!');
                            $state.go('login.signin');
                        }
                    })
                    .catch(function(error) {
                        toastr.error(error.message, error.reason, { timeOut: 10000 });
                        $scope.user = {};
                    })
            }
        };
}]);

this console.log("pseudonym: ", pseudonym); gives me what I want to access, but can't access it from other views, by just typing {{pseudonym}} for example.

Assign to a $scope variable, whenever you want to display on view ,

pseudonym = snapshot.val().pseudonym;
$scope.pseudonym =pseudonym;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM