[英]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.