ngFor 不显示数据 Ionic 4

ngFor not showing data Ionic 4

我试图在 *ngFor 中循环我的数据,但它没有显示数据,但在我的控制台中显示了。下面是我试过的代码。

api.service.ts

    getOrder(orderId): Observable<CustomerOrder> {
       return this.http
      .get<CustomerOrder>(this.apiUrl + 'OrderProduct/' + orderId)
      .pipe(
        retry(2),
        catchError(this.handleError)
      )
  }

orderdetails.component.ts

orderData: any[] = [];

ngOnInit() {
   this.orderId = this.activatedRoute.snapshot.params.orderId;
   this.apiService.getOrder(this.orderId).subscribe(res => {
  Object.assign(this.orderData, res);
  console.log(res);
});
}

orderdetails.page.html

<ion-card *ngFor="let ord of orderData">
      <ion-card-title>
        <h4 class="ion-text-center" style="font-weight: bold">{{ord.Customer?.Name}}</h4>
        <h5 class="ion-text-center">{{ord.OrderNo}}</h5>
      </ion-card-title>
      <ion-card-content>
       <ion-list>
          <ion-item>
            <p style="font-weight: bold">Items: </p>&nbsp;
            <ion-label>{{ord.OrderProducts?.Product}}</ion-label>
          </ion-item>
          <ion-item>
            <p style="font-weight: bold">Quantity: </p>&nbsp;
            <ion-label>{{ord.Quantity}}</ion-label>
          </ion-item>
          <ion-item>
            <p style="font-weight: bold">Item Price: </p>&nbsp;
            <ion-label>{{ord.ItemPrice}}</ion-label>
          </ion-item>
        </ion-list>
       </ion-card-content>
      <ion-button type="button" color="primary"
                  style="float: right; padding-right: 5px;padding-bottom: 3px;" 
        (click)="closeOrder()">
        Close
      </ion-button>
      <ion-button type="button" color="danger"
                  style="float: left; padding-left: 5px;padding-bottom: 3px;">
        Cancel
      </ion-button>

    </ion-card>

在控制台中它显示下面的数据是

这实际上与 Ionic 无关,都是关于 Angular 中的变化检测。

这是因为您在呈现页面之前没有等待数据 return。

对于代码的当前状态,最简单的方法是像这样用 NgContainer 包装 ion-card 元素。

<ng-container *ngIf="orderData.length>1">
add you card HTML here
</ng-container>

数据变化会触发a change detection which will cause the page to re-render.

但是由于您使用的是可观察对象...更好的方法是:

  1. 向您的 HTML 添加异步侦听器,它使用 returned 数据:
<ng-container *ngIf="orderData$ | async as orderData">
<ion-card *ngFor="let ord of orderData">
      <ion-card-title>
        <h4 class="ion-text-center" style="font-weight: bold">{{ord.Customer?.Name}}</h4>
        <h5 class="ion-text-center">{{ord.OrderNo}}</h5>
      </ion-card-title>
      <ion-card-content>
       <ion-list>
          <ion-item>
            <p style="font-weight: bold">Items: </p>&nbsp;
            <ion-label>{{ord.OrderProducts?.Product}}</ion-label>
          </ion-item>
          <ion-item>
            <p style="font-weight: bold">Quantity: </p>&nbsp;
            <ion-label>{{ord.Quantity}}</ion-label>
          </ion-item>
          <ion-item>
            <p style="font-weight: bold">Item Price: </p>&nbsp;
            <ion-label>{{ord.ItemPrice}}</ion-label>
          </ion-item>
        </ion-list>
       </ion-card-content>
      <ion-button type="button" color="primary"
                  style="float: right; padding-right: 5px;padding-bottom: 3px;" 
        (click)="closeOrder()">
        Close
      </ion-button>
      <ion-button type="button" color="danger"
                  style="float: left; padding-left: 5px;padding-bottom: 3px;">
        Cancel
      </ion-button>
</ion-card>
</ng-container>
  1. 然后在你的打字稿文件中:
orderData$: Observable<Record<string, unknown>[]>;

ngOnInit() {
   this.orderId = this.activatedRoute.snapshot.params.orderId;
   this.orderData$ = this.apiService.getOrder(this.orderId);
}

我修好了。在ts文件中,我是这样包装数据的。

 this.apiService.getOrder(this.orderId).subscribe(res => {
  this.orderData = [res];
   console.log(res);
});