V
V
Valeriy Donika2015-11-06 00:40:42
Angular
Valeriy Donika, 2015-11-06 00:40:42

Run a function in an Angular.js run block?

After server reboot. The session on the server is lost. And the cookie remains on the client. I want to log in the user once when starting the application.
MainController it's my parent over ng-view

$rootScope.reLogin = function relog(){
                        console.log('try to login');
                        var userCookie = $cookies.getObject('user');
                        if(!userCookie){
                            return false;
                        }
                        var user = {username: userCookie.username , password: userCookie.password};
                        authService.login(user)
                            .then(function(response){
                                    if(response.state === 'success'){
                                        return true;
                                    }
                                    else{
                                        $rootScope.authenticated = false;
                                        $rootScope.current_user = {};
                                        $rootScope.error_message = response.message;
                                        $cookies.remove('user');
                                    }
                                },
                                function(response){
                                    $rootScope.error_message = "Some error with login " + response.status;
                                }
                            );
                    };

Then in main app.js in run try to use
$rootScope.reLogin();
Writes - Uncaught TypeError: $rootScope.reLogin is not a function
If I put a function in
$rootScope.$on('$routeChangeSuccess', function() {});

That's all right, but it's stupid to run a check every time. How to do it once? :)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question