1

I have my pseudonym in the $scope and I try to access it from other views after the user has logged in, using:
however, when I refresh the page immediately after the user has successfully signed in, the $scope value reverts back into {{pseudonym}} and the parameter isn't available. How can I save this data persistently throughout the logged in session?
Can you please provide the answer with an example?

app.controller("MyregisterCtrl", ["$scope", "$stateParams", "Auth", "$state", "$location", "$modal", "DatabaseRef", "$rootScope",
    function ($scope, $stateParams, Auth, $state, $location, $modal, DatabaseRef, $rootScope) {   
        $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) {

                        var userId = firebase.auth().currentUser.uid;
                        DatabaseRef.ref('/users/' + userId).once('value')
                            .then(function(snapshot) {
                                pseudonym = snapshot.val().pseudonym;
                                console.log("pseudonym: ", pseudonym);
                                $scope.pseudonym = pseudonym;
                            });
                        $state.go('app.dashboard');

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

1 Answer 1

1

You should use a Service to store the value and retrieve it whenever you need.

var myApp = angular.module('myApp',[]);
myApp.service('mySingleton', function() {
    var username= "test";
    return {
        username : username
    };
});

function MyCtrl($scope, mySingleton) {
    $scope.username= mySingleton.username;
}

function MyCtrl2($scope, mySingleton) {
    $scope.username= mySingleton.username;
}
Sign up to request clarification or add additional context in comments.

4 Comments

could you please tell me what part of my code in the login controller should I store in the service? Should I completely remove it from login controller and put it in service? or both?
wait let me show, check this sample plnkr.co/edit/9FUSD4LyETI4itmfBtwK?p=preview
the example is a bit difficult to relate to my original controller. Is it possible to give an adapted response to my controller?
what you have done is right here, stackoverflow.com/questions/40058330/… mark as answer if it has helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.