循环后隐藏IonicLoading

时间:2016-07-20 10:28:40

标签: ionic-framework

在我的控制器中,我有以下代码:

$scope.auth = function (s) {

    $ionicLoading.show({
        content: 'Loading',
        animation: 'fade-in',
        showBackdrop: true,
        maxWidth: 200,
        showDelay: 0
    })

    if (s=0) {
        console.log("Alert!")
    } else {

        for (i = 0; i < s; i++) {
            Data.update("my params").then(function (result) {
                console.log(result)
            }, function (err) {
                console.log(err);
            });
        }

    }
}

Data.update()是services.js文件中的http请求。

我希望显示ionicLoading直到循环结束。如果我在循环后放置$ ionicLoading.hide()没有任何反应,因为加载器会立即显示和隐藏。

你能帮助我吗?

2 个答案:

答案 0 :(得分:0)

使用$q.all同时触发多个承诺,并在所有承诺解决后执行某些操作:

var promiseArray = [];

for (i = 0; i < s; i++) {
  promiseArray.push(Data
    .update("my params")
    .then(function (result) {
       console.log(result)
     }, function (err) {
       console.log(err);
    });
  });
}

$q
  .all(promiseArray)
  .then(function() {
    $ionicLoading.hide();
  });

答案 1 :(得分:0)

您可以在for循环的最后一次迭代中隐藏加载

for (i = 0; i < s; i++) {
        Data.update("my params").then(function (result) {
            if(i==s){
               $ionicLoading.hide();
            }
            console.log(result)
        }, function (err) {
            console.log(err);
        });
    }