【问题标题】:How to call $urlRouterProvider.otherwise() after loading JSON file?加载 JSON 文件后如何调用 $urlRouterProvider.otherwise()?
【发布时间】:2016-03-02 04:24:09
【问题描述】:

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

$.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');
   }
 }

【问题讨论】:

  • 我不确定这是否可行,但你可以做的是在 getJSON 代码周围添加一个超时,这将确保它首先运行。
  • $urlRouterProvider.otherwise 在 setTimeout 函数中不起作用
  • 使用angular提供的$timeout

标签: angularjs ionic-framework


【解决方案1】:

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

AngularJS 生命周期分为 2 个阶段配置(您可以在其中使用提供者,但不能使用服务,因为这些尚未注册) , 然后运行(这里不能使用providers,但是可以使用services,一般相当于main函数)。

运行阶段在配置阶段完成后开始,配置阶段不等待任何异步进程,所以发生的情况是当你的 JSON 得到承诺解决了你的配置阶段已经完成(因此您在承诺成功回调中尝试执行的任何提供程序配置都不会真正配置任何内容)。

简而言之,您不能像 getJson 方法那样使用 $urlRouterProvider.otherwise() 传递异步调用的结果。

您尝试执行的操作(根据身份验证重定向用户)的几种替代方法是: angular ui-router login authenticationangularjs redirect to login page if not authenticated with exceptions

【讨论】:

    【解决方案2】:

    考虑使用$state.go('someState')

    您希望您的代码如下所示:

    if(access_token){
                $state.go('myJobs'); // where myJobs is the state correlated with your url 'tabs/jobs'
            }else{
                $state.go('login'); // or whatever the state name is that you have for 'login'
            }
       }else{
           console.log("in line 282");
            $state.go('login');
       }
    

    【讨论】:

    • 我尝试过使用 $state.go()。但它给出了错误'$state is not defined'。
    • 你在控制器中定义 $state 吗?
    • 我在 app.config 函数中有所有路由。我想在相同的功能中使用 $state。我可以用吗?
    • 你应该可以的。
    • 让我知道这是否适合您。如果您遇到问题,请发送plunker
    【解决方案3】:

    如果您尝试有条件地将用户发送到不同的路由,请使用$state.go('route.path') 而不是更新.otherwise() 配置。例如:

    var app = angular.module('myapp',['ionic']);
    
    app.controller('$scope', '$state','$http', [function($scope, $state, $http){
       $http.get('my/api/path')
       .then(function(response){
            if(response.authenticated){
                $state.go('secret.route');
            } else {
                $state.go('public.route');
            }
       });
    }]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2022-08-22
      • 2017-05-08
      • 2016-05-25
      相关资源
      最近更新 更多