简体   繁体   English

*ngIf 用于异步服务功能

[英]*ngIf for async service function

I am starting with IONIC+Javascript+Async functions and having a situation that I'm stuck with.我从 IONIC+Javascript+Async 函数开始,遇到了我遇到的情况。

I have an "index" page that will load data from a service and show it with ngFor.我有一个“索引”页面,它将从服务加载数据并用 ngFor 显示它。 If there is no data, I want to show an ion-row saying No Media Found.如果没有数据,我想显示一个 ion-row 说 No Media Found。 Only if there is no data from the service it is going to be shown.只有在没有来自服务的数据时才会显示它。

Here is my relevant part of the home.page.ts这是我的 home.page.ts 的相关部分


  constructor(private mediaSvc: MediaService){
this.loading = true;
}

  ionViewDidEnter() {
    console.log("ionViewDidEnter before getMedia - loading ", this.loading);
    this.getMedia();
    console.log("ionViewDidEnter after getMedia - loading ", this.loading);
  }

  getMedia() {
    console.log("getMedia inside");
    this.mediaSvc.getMedia().then(result => {
      this.arr = result || [];
      this.loading = false;
      console.log("getMedia inside promisse - loading ", this.loading);
    });
  }

And here is my relevant code inside home.page.html这是我在 home.page.html 中的相关代码

<ion-grid class="ion-no-padding">

    <ion-row>
      <ion-col>
        <ion-list>
          <ion-item *ngFor="let data of arr">
            <ion-icon name="film" slot="start"></ion-icon>
            <div tappable>
              <h2>
                <strong>{{data.name}}</strong>&nbsp;/&nbsp;Episode:
                {{data.episode}}
              </h2>
              <h3>
                Film Period:
                <em>
                  <strong>{{ data.startDate | date: "MMMM dd, yyyy"}}</strong>
                  to
                  <strong>{{ data.endDate | date: "MMMM dd, yyyy"}}</strong>
                </em>
              </h3>
            </div>
            <div slot="end">
              <ion-button fill="clear" (click)="showOptions(data)">
                <ion-icon name="ellipsis-vertical-sharp"></ion-icon>
              </ion-button>
            </div>
          </ion-item>
        </ion-list>
      </ion-col>
    </ion-row>

    <ion-row *ngIf="!loading">
      <ion-col class="ion-text-center">
        <ion-text><h2>No media found!</h2></ion-text>
      </ion-col>
    </ion-row>
  </ion-grid>

Inside the media.service.ts在 media.service.ts 里面

getMedia() {
    return new Promise<any[]>(resolve => {
      let arr = [];

      let fakeData = {};

      fakeData = {
        id: 1,
        name: "Test 1",
        description: "This is a fake test",
        startDate: "January 1, 2020",
        endDate: "",
        episode: 1,
        slates: []
      };

      arr.push(fakeData);

      fakeData = {
        id: 2,
        name: "Test 2",
        description: "This is a fake test 2",
        startDate: "January 1, 2010",
        endDate: "December 31, 2011",
        episode: 1,
        slates: []
      };

      arr.push(fakeData);

      resolve(arr);
    });
  }

What happens is, as the mediaSvc.getMedia is async, at the time I test if !loading, it is still true.发生的情况是,由于 mediaSvc.getMedia 是异步的,在我测试 !loading 时,它仍然是正确的。 I need it false (not loading anymore, I set it to false on my getMedia).我需要它 false (不再加载,我在我的 getMedia 上将它设置为 false)。

The results I'm getting are always showing the "No Media Found" after the data being loaded.加载数据后,我得到的结果总是显示“未找到媒体”。

I have this result with my console logs.我的控制台日志有这个结果。

ionViewDidEnter before getMedia - loading  true
getMedia inside
ionViewDidEnter after getMedia - loading  true
getMedia inside promisse - loading  false

How do I deal with this?我该如何处理? I mean, how can I test these conditions for true/false if it is async?我的意思是,如果是异步的,我如何测试这些条件的真/假? Moreover, is there another approach to this situation?此外,是否有另一种方法来解决这种情况?

I'm starting with this stack, so I might be missing something simple.我从这个堆栈开始,所以我可能会遗漏一些简单的东西。

When you retrieve the data, you then set loading to false.当您检索数据时,然后将加载设置为 false。 In the html, you have the if statement set so that if loading is false, it displays the 'No media found' message.在 html 中,您设置了 if 语句,以便在加载为 false 时显示“未找到媒体”消息。

So this is correct behaviour for the logic.所以这是逻辑的正确行为。

You want the message to show if there are no items returned.如果没有返回任何项目,您希望消息显示。 This could be done with:这可以通过以下方式完成:

<ion-row *ngIf="!arr || arr.length == 0">
      <ion-col class="ion-text-center">
        <ion-text><h2>No media found!</h2></ion-text>
      </ion-col>
    </ion-row>

See StackBlitz demo (comment out all arr.push(fakeData); lines in media.service to see 'No items found' message).请参阅StackBlitz 演示(注释掉media.service 中的所有arr.push(fakeData);行以查看“未找到项目”消息)。

try this尝试这个

 constructor() {
     this.loading = true;
 }

in html在 html 中

 <ion-row *ngIf="loading">
    <ion-col class="ion-text-center">
      <ion-text><h2>No media found!</h2></ion-text>
    </ion-col>
</ion-row>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM