如何在加载JSON文件后调用$ urlRouterProvider.otherwise()? [英] How to call $urlRouterProvider.otherwise() after loading JSON file?

查看:273
本文介绍了如何在加载JSON文件后调用$ urlRouterProvider.otherwise()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于离子和角度js的项目。我正在加载JSON文件,其中包含键值对中的一些JSON数据。我想要实现的是在完全加载json文件后我必须调用$ urlRouterProvider.otherwise()方法。以下是我尝试的代码,但它对我不起作用。我已经尝试将控制台放在'defaultRoute'函数中,它正在执行但是'$ urlRouterProvider.otherwise('/ tab / myjobs')'这一行不起作用。以下代码出现在app.config函数中。任何帮助将不胜感激。

I am working on a project based on ionic and angular js. I am loading JSON file which contains some JSON data in key-value pair. What I want to achieve is I have to call $urlRouterProvider.otherwise() method after json file is loaded completely. Following is the code which I have tried but it does not work for me. I have tried putting console in 'defaultRoute' function it is getting executed but '$urlRouterProvider.otherwise('/tab/myjobs')' this line doesn't work.The following code is present in app.config function. Any help will be appreciated.

$.getJSON('js/constants/'+lang+'.json')
        .then(function(response) {
       $translateProvider.translations(window.localStorage['deviceLanguage'],response);
       defaultRoute($urlRouterProvider);
              }, function(response) {
                  //$translate.use('en');
      });


function defaultRoute($urlRouterProvider){
             if(window.localStorage['userData']) {
        var access_token = JSON.parse(window.localStorage['userData']).access_token;
        if(access_token){
            $urlRouterProvider.otherwise('/tab/myjobs');
        }else{
            $urlRouterProvider.otherwise('/login');
        }
   }else{
       console.log("in line 282");
        $urlRouterProvider.otherwise('/login');
   }
 }


推荐答案

问题是您在配置阶段运行异步方法

AngularJS生命周期分为 2阶段配置(您可以使用提供商,但不提供服务,因为这些尚未注册),并运行 (你不能使用提供者,但你可以使用服务,一般来说等同于主要功能)。

AngularJS lifecycle is splitted in 2 phases, config (where you can use providers, but not services because these are not yet registered), and run (where you cannot use providers, but you can use services and in general is equivalent to the main function).

运行阶段在配置阶段结束时开始,并且 config阶段不等待任何异步进程,所以正在发生的是当你的JSON获得承诺解决后你的配置阶段已经完成(所以任何提供者配置你尝试做你的承诺成功回调不真正配置任何东西)。

The run phase starts when config phase has finished, and config phase do not wait for any async process, so what is happening is that when your JSON get promise is solved your config phase has already finished (so any provider config you try to do in your promise success callback do not really config anything).

所以简而言之,你不能使用$ urlRouterProvider.otherwise()传递res超级异步调用,就像你的getJson方法一样。

您尝试做的几种替代方法(根据auth重定向用户)是:
角度ui-router登录身份验证angularjs重定向到登录页面,如果未通过例外验证

A couple alternatives to what you are trying to do (redirect user depending on auth) are: angular ui-router login authentication and angularjs redirect to login page if not authenticated with exceptions.

这篇关于如何在加载JSON文件后调用$ urlRouterProvider.otherwise()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆