简体   繁体   中英

AngularJS autologin before page is loaded

I try to implement a remember-login function to my AngularJS / ionic mobile app

the problem: the login page is allways loaded before the automatic login is finished and the transition starts, so you can see the login page for a moment.

question: is there an event like 'pagebeforeshow' in JQM or another method to directly load the 'home' state?

I use: ionic 1.3.1 angular 1.5.3 cordova 4.0.0

code examples: LoginController:

function LoginController($scope, $http, $ionicModal, $state, $SessionStorage) {
    var vm = this;
    activate();

    function activate(){
        vm.staylogged = JSON.parse(localStorage.staylogged || null);
        if(vm.staylogged){
            vm.remember = vm.staylogged; //vm.remember: checkbox in login form
            login();
        }else{
            // get some data from the server
        }
    }

    function login(){
        if(vm.staylogged){
            //get login informations from localStorage
        }

        $http({
            method: 'POST',
            url: vm.server +"?=GetLogin",
            headers: {
                'Content-Type': 'text/xml; charset=\"utf-8\"'
            },
            data: soa
        }).then(function successCallback(response) {
            if(response.returnCode == 0){
                localStorage.setItem('staylogged', JSON.stringify(vm.remember));
                // safe login informations to local Storage for next use
                $state.go('tabs.home');
            }
        }, function errorCallback(response) {
            console.log(response);
        });
    }
}

You can do following way -

In app.js

.state('signin', {
       url: '/sign-in',
       templateUrl: 'templates/views/login.html',
       controller: 'LoginController',
       resolve: {
               // Do your code here will execute before page render
       }
})

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