How to finish a loading screen after running Presets in Angular?

Asked

Viewed 375 times

1

Good morning.

I have a question, I have a controller that searches some data of a webservice Rest, and persists them in the device database (Ionic), to perform the search of the data, I am using 'precedents'.

When you start the search I open a Loading screen, and I would like that after finishing all the Precedents executions, close the loading screen.

I’m doing it this way:

LoadingService.show();

var promise = function1.promise();
promise.then(function (result) {
    if (result) {
        MeuService.insert(result);
    }
});

var promise2 = function2.promise();
promise2.then(function (result) {
    if (result) {
        MeuService.insert(result);
    }
});

var promise3 = function3.promise();
promise3.then(function (result) {
    if (result) {
        MeuService.insert(result);
     // Já tentei deixar aqui tbm..
    // LoadingService.hide();
    }
});

//??
LoadingService.hide();

The problem is that right after starting, it already closes the loading, and continues the execution of the predecessors. Where am I going wrong?

1 answer

1

You can use the all $q method, which is the service that provides prefixes at angular 1.

As follows:

LoadingService.show();

function insertResult(result) {
  MyService.insert(result);
}

var promise1 = function1.promise();
var promise2 = function2.promise();
var promise3 = function3.promise();

$q.all([
  promise1.then(insertResult),
  promise2.then(insertResult),
  promise3.then(insertResult)
]).then(function() {
  LoadingService.hide();
});

Thus the LoadingService.hide will only be executed after all precedents have been solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.