[英]How To Iterate Multi-Dimensional Array with *ngFor
這里的多維到底是什么? 無論如何,你可以使用*ngFor
來做到這一點,沒有任何問題。 只是一個例子; 假設您的 TS 文件中有一個persons
數組,每個人都可以有多個角色。
<div *ngFor="let person of persons">
<div *ngFor="let role of person.roles">
{{role}}
</div>
</div>
只需確保在 AppModule 中包含 CommonModule:
import { CommonModule } from "@angular/common";
編輯:您的數組不是多維的。 您只能使用一個*ngFor
訪問嵌套屬性:
<div *ngFor="let person of persons">
{{person.company.name}}
</div>
提供的數組不是多維數組。 它只是一個帶有對象的數組。 您可以以正常方式使用 *ngFor。
*ngFor="let user of users"
您可以訪問每個用戶對象及其屬性
{{user. name}}
還有像這樣的內部屬性
{{user.address.street}}
拳頭我想建議你通過https://stackoverflow.com/help/how-to-ask
你的問題的答案是:
TS組件
public objects: Array<any> = [your_array];
HTML 組件
<div *ngFor="let obj of objects">
<div> {{ obj.id }}</div>
<div> {{ obj.name }}</div>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.