记录在用户信息中的angularjs可在整个应用中使用

时间:2016-10-15 07:09:03

标签: javascript angularjs angularjs-scope firebase-realtime-database firebase-authentication

我想在登录后让所有视图都可以使用用户信息。如何修改代码以便能够从其他视图访问pseudonym? 你能举个例子吗?

这是我的登录控制器

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 = {};
                    })
            }
        };
}]);

console.log("pseudonym: ", pseudonym);为我提供了我想要访问的内容,但无法通过输入{{falseonym}}来访问其他视图。

1 个答案:

答案 0 :(得分:1)

只要您想在视图上显示,就可以分配给$ scope变量

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