在angular2中访问嵌套数组对象时出现问题

时间:2019-11-25 20:57:05

标签: angular typescript

如何访问下面给出的数组结构。如果我尝试使用此*ngFor="let item of bookedItems[0]",我认为它只允许我访问 array [0] 数据。那么 array [1],array [2] .... array [n] 呢?我怎么得到他们?

  [Array(2)]
      0: Array(2)
        0: {id: 2, name: "Calark", price: 375, …}
        1: {id: 5, name: "Eternis", price: 450, …}
        length: 2
      __proto__: Array(0)
      length: 1
      __proto__: Array(0)

内部组件

export class BookingComponent implements OnInit {
  bookedItems = [];
  constructor(private bookingService:BookingService) { }

  ngOnInit(){
    this.bookedData();
    console.log(this.bookedItems);
  }

  bookedData(){
    this.bookedItems = this.bookingService.getBookedItem();
  }
}

内部模板

<h3>Your Bookings:</h3>
<div *ngIf="bookedItems.length > 0; else plainText">
  <div class="cart-item" *ngFor="let item of bookedItems[0]">
<div class="flip-card">
  <div class="flip-card-inner">
          <div class="flip-card-front">
            <img [src]="item.imagePath" alt="{{item.name}}" class="img-responsive" style="max-height: 305px" width="293px">
          </div>


          <div class="flip-card-back">
            <small>
                  <p>Name: {{item.name}}</p>
                  <p>Size: {{item.size}}</p>
                  <p>Price: {{item.price}}</p>
                  <p>City: {{item.city}}</p>
                  <p>Address: {{item.address}}</p>
            </small>
          </div>

     </div>
     </div><hr/>
  </div>
</div>


<ng-template #plainText>
  <p>You haven't booked any item. please book to get your booking details!!!</p>
</ng-template>

2 个答案:

答案 0 :(得分:2)

对不起,我没有意识到,该数组是嵌套的。您可以尝试双循环:

  <div class="cart-item" *ngFor="let item of bookedItems">
    <div class="flip-card" *ngFor="let innerItem of item">
      <div class="flip-card-inner">
        <div class="flip-card-front">
          <img [src]="innerItem.imagePath" alt="{{innerItem.name}}" class="img-responsive" style="max-height: 305px" width="293px">
        </div>


        <div class="flip-card-back">
          <small>
              <p>Name: {{innerItem.name}}</p>
              <p>Size: {{innerItem.size}}</p>
              <p>Price: {{innerItem.price}}</p>
              <p>City: {{innerItem.city}}</p>
              <p>Address: {{innerItem.address}}</p>
          </small>
        </div>

     </div>
    </div><hr/>
 </div>

如果遇到此问题,考虑到只有一层嵌套,我将首先将数组展平。

答案 1 :(得分:2)

我认为您这里肯定需要一个双循环,除非您有嵌套数组是一个错误。如果不是这样,您可能应该使用“ ng-container”指令,例如=>

<h3>Your Bookings:</h3>
<div *ngIf="bookedItems.length > 0; else plainText">
  <ng-container *ngFor="let bookedItem of bookedItems">
    <div class="cart-item" *ngFor="let item of bookedItem">
      <div class="flip-card">
        <div class="flip-card-inner">
          <div class="flip-card-front">
            <img [src]="item.imagePath" alt="{{item.name}}" class="img-responsive" style="max-height: 305px"
                 width="293px">
          </div>
          <div class="flip-card-back">
            <small>
              <p>Name: {{item.name}}</p>
              <p>Size: {{item.size}}</p>
              <p>Price: {{item.price}}</p>
              <p>City: {{item.city}}</p>
              <p>Address: {{item.address}}</p>
            </small>
          </div>
        </div>
      </div>
      <hr/>
    </div>
  </ng-container>
</div>


<ng-template #plainText>
  <p>You haven't booked any item. please book to get your booking details!!!</p>
</ng-template>

希望它能对您有所帮助。