首页 > 解决方案 > 如何使用 Angular 在 HTML 中访问没有键名的数组对象

问题描述

我正在使用 Angular 7 和 Spring boot 5 开发一个网页。

所以,使用服务,我有这个数组数组:

在此处输入图像描述

我想在 html 组件的表格中显示它。我不知道如何访问每个项目(例如“Estanteria 2”),因为它没有键名。

有没有办法在没有键名的情况下访问 html 中的这些数据?

标签: htmlarraysangulartypescript

解决方案


这将取决于您希望如何在视图中显示它。

你有一个数组数组。所以要Estanteria 2在 HTML 中显示,这样的事情就可以了;

<div *ngFor="let item of array">
  <!-- this will show Estanteria 2 -->
  <div>{{ item[3] }}</div>
</div>

如果你想遍历第二个数组中的所有值,你可以:

<div *ngFor="let item of array">
  <!-- this will show all the values in the second array -->
  <div *ngFor="let value of item">{{ value }}</div>
</div>

推荐阅读