【问题标题】:How to print all data in angular html page usigng ngFor如何使用 ngFor 在 Angular html 页面中打印所有数据
【发布时间】:2020-09-27 06:00:30
【问题描述】:

我有一个如下的数据集合,我需要使用 ngFor 循环在 HTML 页面上打印这些信息。

[
  {_id: "5ed387946094abebeaa6e75a", name: "Andaman and Nicobar Islands", active: 0, cured: 33, death: 0},
  {_id: "5ed387946094abebeaa6e75b", name: "Andhra Pradesh", active: 1654, cured: 2576, death: 73},
  {_id: "5ed387956094abebeaa6e75c", name: "Arunachal Pradesh", active: 44, cured: 1, death: 0},
  {_id: "5ed387966094abebeaa6e75d", name: "Assam", active: 1651, cured: 498, death: 4},
  {_id: "5ed387966094abebeaa6e75e", name: "Bihar", active: 2342, cured: 2225, death: 29},
  {_id: "5ed387966094abebeaa6e75f", name: "Chandigarh", active: 77, cured: 222, death: 5},
  {_id: "5ed387976094abebeaa6e760", name: "Chhattisgarh", active: 633, cured: 244, death: 2}
]

【问题讨论】:

    标签: html angular


    【解决方案1】:

    选项 1:json 管道

    <ng-container *ngFor="let item of state">
      {{ item | json }}
    </ng-container>
    

    选项 2:keyvalue 管道

    <ng-container *ngFor="let item of state">
      <ng-container *ngFor="let property of item | keyvalue">
        {{ property.key }}: {{ property.value }}
      </ng-container>
    </ng-container>
    

    选项 3:直接访问属性

    <ng-container *ngFor="let item of state">
      {{ item?.name }}
      {{ item?.active }}
      {{ item?.cured }}
      {{ item?.death }}
      ...
    </ng-container>
    

    【讨论】:

    • 找不到名为“json”的管道。
    • 如果 Angular stackoverflow.com/q/40389309/6513921。如果 Angular > V9 见这里:github.com/angular/angular/issues/26436#issuecomment-455841125
    • core.js:12565 无法绑定到“ngForOf”,因为它不是“ng-container”的已知属性。
    • 见这里:stackoverflow.com/a/40331563/6513921。模块需要在组件所在的模块中导入。
    • 您需要显示更多详细信息。应用程序和模块文件的结构。没有它就变成了猜测。
    猜你喜欢
    • 2018-07-13
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    相关资源
    最近更新 更多