简体   繁体   English

页面加载前AngularJS自动登录

[英]AngularJS autologin before page is loaded

I try to implement a remember-login function to my AngularJS / ionic mobile app 我尝试对我的AngularJS / ionic移动应用程序实现一个记住登录功能

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? 问题: JQM中是否有类似“ pagebeforeshow”的事件或直接加载“ home”状态的其他方法?

I use: ionic 1.3.1 angular 1.5.3 cordova 4.0.0 我使用:离子1.3.1角1.5.3科尔多瓦4.0.0

code examples: LoginController: 代码示例: 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 在app.js中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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