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

印度Stackoverflow

我如何访问下面给出的数组结构。如果我尝试这样做,*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>
千禧年

我认为您在这里肯定需要一个双循环,除非您有嵌套数组的事实是一个错误。如果不是这样,您应该使用“ 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>

希望对您有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Javascript对象;访问json数组时出现问题

从json检索嵌套数据时出现问题

比较数组中的JavaScript对象时出现问题

在 Angular 5 中刷新访问令牌时出现问题

在Angular2中使用PhoneGap,加载模板时出现问题

使用 .includes 检查用户输入(测验问题的答案)是否与嵌套数组中的正确答案之一匹配时出现问题

对象Angular 2+ ngrx中的嵌套数组

Angular:访问 *ngFor 循环中的对象数组时出现问题,而字符串插值在 html 中显示数据,但错误 ctx 在控制台中未定义

ReactJS和axios,从JSON响应访问嵌套对象时出现问题

使用把手访问数组中所有对象的特定元素时出现问题

如何从对象数组中的嵌套数组访问特定值?

通过Powershell中的变量解析/访问嵌套的JSON /哈希表数据时出现问题

如何在angular2的表单数组中显示嵌套数组数据?

遍历嵌套 JavaScript 数组时出现问题

在流星模板中访问嵌套数组中的对象

访问对象javascript getter时出现问题

由于int导致访问json数组时出现问题

Swift:似乎无法访问嵌套数组中的对象

如何访问嵌套数组中的对象的属性?

使用Go访问嵌套数组和对象中的数据

如何在 React 中访问嵌套数组对象

Angular2。当我使用Pipe时,* ngFor出现问题

Swift 2D数组通用扩展-访问第二维时出现问题

JS:从封闭函数内部访问对象中的数据时出现问题

向数据帧添加新行(其中包含嵌套数据帧)时出现问题

在位于对象键中的数组中推送值时出现问题

对象中的嵌套数组

写入和读取对象数组C ++时出现问题

将对象转换为过滤数组时出现问题