javascript - Angular 5 嵌套数组

标签 javascript angular angular5

我正在尝试在嵌套 json 输出上使用 ngfor。第一个循环工作正常,但内部循环不工作。我的代码是

Ts 文件

import { Component, OnInit } from '@angular/core';
import { TiffinService } from '../tiffin.service';
import { IVen } from '../vendor';

@Component({
  selector: 'app-tabletest',
  templateUrl: './tabletest.component.html',
  styleUrls: ['./tabletest.component.css']
})
export class TabletestComponent implements OnInit {
  vend: IVen[];

  constructor(private apiService: TiffinService) { }

  ngOnInit(){
    this.apiService.getVendor('1')
          .subscribe(
          resultArray => {
          this.vend = resultArray;
        });
  }
}

服务的输出是

[{"Cat":[{"Catname":"Daily","Catprice":15.99},{"Catname":"Weekly","Catprice":99.99}],"Vaddress1":"vad1","Vaddress2":"vad2","Vcell":"123456789","Vcity":"milton","Vemail":"v@ven.ca","Vid":1,"Vname":"vendor1","Vpcode":"l9t0p2","Vphone":"123456789"},{"Cat":[{"Catname":"Daily","Catprice":15.99},{"Catname":"Weekly","Catprice":110.99}],"Vaddress1":"vad1","Vaddress2":"vad2","Vcell":"123","Vcity":"milton","Vemail":"v@ven.ca","Vid":2,"Vname":"vendor2","Vpcode":"l9","Vphone":"123"}]

html 文件是

<div *ngFor="let item of vend">
   <table>

      <ng-container>
        <tr>
          <td colspan="2">{{item.Vname}}</td>
        </tr>
        <tr *ngFor="let value of vend.Cat">
          <td>{{value.Catname}}</td>
        </tr>
      </ng-container>
   </table>
<div>

最佳答案

您需要访问 item.Cat 而不是从父循环检索的 vend

<div *ngFor="let item of vend">
   <table>    
      <ng-container>
        <tr>
          <td colspan="2">{{item.Vname}}</td>
        </tr>
        <tr *ngFor="let value of item.Cat">
          <td>{{value.Catname}}</td>
        </tr>
      </ng-container>
   </table>
<div>

STACKBLITZ DEMO

关于javascript - Angular 5 嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53348987/

相关文章:

angular5 - 从 Angular 4 更新到 Angular 5 后自定义 FormControl 失败

javascript - 想要使用 javascript 从 css url 获取图像,这样我就可以设置背景图像的样式

javascript - 检查 typescript 类是否有 setter/getter

angular - 将 Angular 从版本 5 更新到 6

angular - 模块 'MatSnackBar' 导入了意外值 'AppModule'。请添加@NgModule 注解

angular - 什么时候调用计算元素宽度的函数?

javascript - 时刻时区 - 无效日期

javascript - 未捕获( promise )TypeError : Cannot read property 'createElement' of undefined(…)

javascript - Typescript/Angular 中的 IF 语句

javascript - ngx-datatables 在排序时给出 "rxjs_1.fromEvent is not a function"错误