简体   繁体   English

AngularJS:从$ location到$ route

[英]AngularJS: From $location to $route

Hey guys I changed my angular app to work with the ui.router directive, but I need to change the code I already had made with $location, Changed everything already but this part of the code, 大家好,我更改了我的角度应用程序以使用ui.router指令,但是我需要更改我已经用$ location制作的代码,已经更改了除了代码的这一部分之外的所有内容,

app.run(function($rootScope,$location,checkLogin,User){
    $rootScope.$on("$routeChangeStart",function(event,next,current){
        checkLogin.check(function(response){
            if (response) {
                var nextUrl = next.$$route.orginalPath

                if (nextUrl == '/login' || nextUrl == '/') {
                    $location.path('panel');
                }
                $rootScope.isLogged =true;
            }else{
                    $rootScope.isLogged =false;
                    $location.path('login');

                }
        });
    });
});

I need to transform the next.$$route.orginalPath from $location to its $state alternative? 我需要将next。$$ route.orginalPath从$ location转换成其$ state替代方案吗? Sorry for bad english, long story short I need to update my code from $location to $state 对不起,英语不好,长话短说,我需要将代码从$ location更新为$ state

$state.go('panel', {'parameter_name': parameter_value}); $ state.go('panel',{'parameter_name':parameter_value});

The first parameter 'panel' is the $stateProvider.state route name (not the url state) and the second parameter are the route paremeters if they have been defined. 第一个参数“ panel”是$ stateProvider.state路由名称(不是url状态),第二个参数是路由参数(如果已定义)。

I can see that you are validating if the user has an active session each time he enters to the route, this is not a good practice. 我可以看到您正在验证用户每次进入该路由时是否都具有活动会话,这不是一个好习惯。 You should validate user session on your api rest service each time he requests to the server (this is possible using an angular http interceptor. Using interceptors allows you to do something at sending or at receiving the request. 每次用户请求服务器时,都应在api rest服务上验证用户会话(使用有角度的http拦截器可以实现此目的。使用拦截器可让您在发送或接收请求时执行某些操作。

here a demo: http://i.prntscr.com/IWszLQxwQ9ic09sgyJRpvQ.png ). 这是一个演示: http : //i.prntscr.com/IWszLQxwQ9ic09sgyJRpvQ.png )。

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

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