How to call $ urlRouterProvider.otherwise () after loading a JSON file?

I am working on a project based on ionic and angular js. I am loading a JSON file containing some JSON data in key-value pairs. What I want to achieve is a call to the $ urlRouterProvider.otherwise () method after the json file is fully loaded. Below is the code I tried, but it does not work for me. I tried putting the console into the defaultRoute function that it executes, but this line doesn’t work with $ urlRouterProvider.otherwise ('/ tab / myjobs'). The following code is present in the app.config function. Any help would 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');
   }
 }
0
source share
3 answers

Problem , async .

AngularJS 2 , config ( , , ), ( , ).

, , - , , , JSON , ( , , ).

, $urlRouterProvider.otherwise(), , , getJson.

, ( auth): angular ui-router angularjs , .

+1

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

, $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');
        }
   });
}]);
0

Source: https://habr.com/ru/post/1667067/


All Articles