8

我正在尝试以这种方式使用最近添加的 LoadingController:

let loading=this.load.create({
  content: "Connexion au serveur Migal en cours..."
});

loading.present();

this.http.get(this.urlCheckerForm.value.migalUrl+'/action/MobileApp/URLChecker')
  .map(res => res.json())
  .subscribe(
    data => this.newConnection(data,loading),
    error => this.catchURLError(loading));

loading.dismiss();

基本上,我只想在我的页面加载之前显示我的加载弹出窗口,然后将其关闭。

我遵循了Ionic 2 Documentation上的示例,但它似乎根本不起作用......

编辑:加载组件甚至没有出现。

4

4 回答 4

15

您的代码的问题是您正在发出 http 请求,然后调用dismiss()但该dismiss()方法不会等待 http 请求完成。请看一下这个plunker

代码几乎是不言自明的:

import { NavController, LoadingController } from 'ionic-angular/index';
import { Http, Response } from '@angular/http';
import { Component } from "@angular/core";
import 'rxjs/Rx';

@Component({
  templateUrl:"home.html"
})
export class HomePage {

  private users : Array<any>

  constructor(private http: Http, private loadingCtrl: LoadingController) {

    // Create the popup
    let loadingPopup = this.loadingCtrl.create({
      content: 'Loading data...'
    });

    // Show the popup
    loadingPopup.present();

    // Get the data
    this.http.get('https://jsonplaceholder.typicode.com/users')
      .map((res: Response) => res.json())
      .subscribe(
        data => {

          // I've added this timeout just to show the loading popup more time
          setTimeout(() => {
            this.users= data;
            loadingPopup.dismiss();
          }, 1000);

          // Should be just this:
          // this.users= data;
          // loadingPopup.dismiss();
        },
        err => console.error(err)
    );

  }
}
于 2016-08-22T17:23:18.387 回答
1

我把“loading.dismiss();” 在订阅 {} 中,它运行良好:)

                    this.http.get(url)
                        .subscribe(data =>{
                            this.items=JSON.parse(data['_body']).results;
                            loading.dismiss();
                        },
于 2017-04-17T09:39:51.463 回答
1

您可以使用 ionic ionViewLoaded() 弹出加载控制器,然后在加载视图并从您的订阅中获取内容时将其关闭。

ionViewLoaded() {
  let lr = this.loading.create({
    content: 'Your Display Message...',
  });

  lr.present().then(() => {
    this.Yourservice.Yourfunction()
      .subscribe(res => {
        this.yourresult = res;
      });
    lr.dismiss();
  });
}

您还可以像这样构建您的代码,以确保在您关闭加载程序之前订阅已完成。

ionViewLoaded() {
      let lr = this.loading.create({
        content: 'Your Display Message...',
      });

      lr.present().then(() => {
        this.Yourservice.Yourfunction()
          .subscribe(
          data => this.yourresult = data,
          error => console.log(error),
          ()=> lr.dismiss()
          );

      });
    }
于 2017-05-23T09:36:04.497 回答
0

对于 ionic 的最后一个版本,您必须处理解除事件:

let loading = this.load.create({
    content: "Connexion au serveur Migal en cours..."
});

loading.present();

loading.onDidDismiss().then(() => {
     do thinks..... 
});
于 2016-08-22T15:04:07.387 回答