0

I want to know if there is a way that lets say controller 2 waits for controller 2 to finish running all of its code. Im making mobile app, when a user logs in, the app goes to the home page which has a tabbar that loads 4 controllers, but they are loading all at the same time, controller 1 loads a part, then controller 2 shows up loading more, problem is that I am making HTTP request to get data and fill the HTML with the data.

aldro_app.controller('ctrTabBarOpciones', function($scope, compartidosFactory, $http, $rootScope){

console.log('controller tabbar');

$scope.comprobarTab = function (tab) {
    compartidosFactory.tab = tab;
}

    var id = parseInt(getVariable("idcontrato"));
    var url = "http://api/info";
    $http({
    url : url,
    method: "POST",
    data: {id_contrato: id},
    headers:{
     "Content-Type":"application/json;charset=utf-8",
     "Authorization": "Bearer " + getVariable("token")
    }
    })
    .then(function(response){
    console.log("Facturas del contrato -> " + id + "son: " + response.data);
    setVariable("facturas" , JSON.stringify(response.data));
    })
  });

this is the first controller that loads (I know it because on the console the first thing to show is "controller tabbar"), and I am saving the data I am getting on localStorage with the setVariable function, problem comes here.

aldro_app.controller('ctrInicio', function ($scope,$http, $rootScope) {
     /* Http para retirar las ultimas facturas del contrato */
     console.log("Inicio controller");
     //console.log(x.facturas.length);
           var objeto = getVariable("facturas");
           deleteVariable("facturas");
           var listado_facturas = JSON.parse(objeto);
           var arr_length = listado_facturas.facturas.length;
      });

I am getting error of Cannot read property 'facturas' of undefined because my ctrTabbarOpciones loads at first but then my ctrInicio shows up running his code that needs the code which has not been executed yet in ctrTabbarOpciones so, how to tell my controller to wait for the other controller? (Sorry for grammar mistakes).

1 Answer 1

1

You can broadcast an event from first controller and listen to it on second controller. 3

aldro_app.controller('ctrTabBarOpciones', function($scope, compartidosFactory, $http, $rootScope){

console.log('controller tabbar');

$scope.comprobarTab = function (tab) {
    compartidosFactory.tab = tab;
}

    var id = parseInt(getVariable("idcontrato"));
    var url = "http://api/info";
    $http({
    url : url,
    method: "POST",
    data: {id_contrato: id},
    headers:{
     "Content-Type":"application/json;charset=utf-8",
     "Authorization": "Bearer " + getVariable("token")
    }
    })
    .then(function(response){
    console.log("Facturas del contrato -> " + id + "son: " + response.data);
    setVariable("facturas" , JSON.stringify(response.data));
        $rootScope.$broadcast('received data'); // broadcast event
    })
  });

aldro_app.controller('ctrInicio', function ($scope,$http, $rootScope) {
     /* Http para retirar las ultimas facturas del contrato */
     console.log("Inicio controller");
     //console.log(x.facturas.length);
     $rootScope.$on('receivedData', function(){
      var objeto = getVariable("facturas");
           deleteVariable("facturas");
           var listado_facturas = JSON.parse(objeto);
           var arr_length = listado_facturas.facturas.length;
     })
          
      });

Sign up to request clarification or add additional context in comments.

3 Comments

My HTTP request in ctrTabBarOpciones is executed after the ctrInicio
Yeah, But you are triggering an event now and listening it in ctrInicio, so it shouldnt cause any problem. Whenever you get the data , it executes the function in ctrInicio
this did it in a way, proble is now that I am trying to access listado_facturas a little later in my ctrInicio, now a dumb question maybe, on my $on event, can I declare another function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.