【问题标题】:ngFor not showing data Ionic 4ngFor 不显示数据 Ionic 4
【发布时间】:2021-06-28 19:47:57
【问题描述】:

我试图在 *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>

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

【问题讨论】:

    标签: angular api rest ionic-framework ionic4


    【解决方案1】:

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

    这是因为你没有在渲染页面之前等待数据返回。

    当前代码状态的最简单方法是使用 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.

    但由于您使用的是 observables...更好的方法是:

    1. 向您的 HTML 添加一个异步侦听器,该侦听器使用返回的数据:
    <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);
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 啊,我现在看到您的订单结果不是数组。您的方法有效,但它有点 hack :) 没有理由迭代您所知道的单一结果。尝试回到旧方法并使用 *ngif="orderData" 并将 orderData 初始化为 null。您可以参考 orderData.OrderId 等。
      猜你喜欢
      • 2017-11-25
      • 2020-09-02
      • 2021-06-03
      • 2019-09-18
      • 2017-01-06
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      相关资源
      最近更新 更多