簡體   English   中英

Angular2單個ngFor用於多個數組

[英]Angular2 single ngFor for multiple arrays

Angular中的ngFor是否可以使用多個數組?

   *ngFor="let device of element.devices && element.zones.devices"// something like this

export interface Element {
    id: string;
    name: string;   
    zones: Zone[];
    devices: Device[];
}

export class Zone {
    id: string;
    name: string;
    devices: Device[];
}

export class Device {
    id: string;
    name: string;
}

我將需要訪問Element對象,因此我需要顯示區域中的所有設備和設備。

使用concat連接兩個數組,然后ngFor使用ngFor

*ngFor = "let device of arr1.concat(arr2.devices)"

您可以如下連接兩個數組

this.elements= arr1.concat(arr2);

此外,為避免發生undefined錯誤,您應該使用 鍵入以下安全操作符

<div *ngFor="let device of elements?.devices">
    <span> {{device?.name}} </span>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM