更改视图上的位置问题angularjs

时间:2018-02-20 08:16:47

标签: angularjs angular-ui-router

遇到一个问题,我必须在位置更改时检查身份验证:

问题

在位置更改时,我请求后端检查auth,如果响应不是401,则显示页面,否则重定向到登录页面,当响应为401时,页面显示一点,然后重定向发生在登录页面,对我来说很头疼,因为页面显示了一点然后位置发生了变化,有人帮帮我吗?

$rootScope.$on('$locationChangeStart', function (event, next, current) {
    var publicPages = [''];


    $rootScope.showPreloader = true;
    AuthenticationService.Login(function (_data) {
        if (_data.result) {
            count  =1;
            localStorage.setItem('userInfo', angular.toJson(_data.result));
            awtcDataService.employeeInfo = _data.result;
            var roles = awtcDataService.employeeInfo
            var route = localStorage.getItem('setRoute')

                if (route == '/check') {
                    $state.go('help');
                }
                else {
                    $state.go('home');
                }

        }
        if (_data.status === 401) {
            $state.go('login', {reload: true})//shows the page e.g home and then redirects to this page
            count  =1;
            event.preventDefault();
            return;
            $timeout(function(){
                console.log("show after directive partial loaded")
            });

        }
        else {

        }
    }, function (error) {
        if (error.status === 401) {
            $state.go('login', {reload: true})/shows the page sometimes e.g home and then redirects to this page
            count  =1;
            event.preventDefault();
            return;
            $timeout(function(){
                console.log("show after directive partial loaded")
            });
        }
    });
});

1 个答案:

答案 0 :(得分:0)

默认情况下,现在不推荐使用所有状态事件(即$ stateChange *和朋友)。而不是侦听事件,请使用Transition Hook API。

https://ui-router.github.io/guide/ng1/migrate-to-1_0#state-change-events