angular - Angular 2 中的嵌套循环

标签 angular

我想显示这个数组的数据:

array = [
{nom: nom1, composants: [composant11, composant12]}, 
{nom: nom2, composants: [composant21, composant22]}
]

我想在表格中这样显示:

成分 名称

Composer 11 名 1

组合物 12 名称 1

合成物 21 名称 2

合成物 22 名称 2

这是我的代码:

<table>
<tr>
<th>Composant</th>
<th>Nom</th>
</tr>
<template *ngFor="let item of array">
<template *ngFor="let composant of item.composants">
<tr>
<td>{{composant.nom}}</td>
<td>{{composant.code}}</td>
</tr>
</template>
</template>
</table>

我的浏览器没有显示任何内容。我想知道是否真的有办法在模板中做到这一点。或者我是否应该在显示之前先转换组件中的数组。 任何建议将不胜感激!

最佳答案

试试这个:

rows = [
    {nom: "nom1", composants: ["composant11", "composant12"]}, 
    {nom: "nom2", composants: ["composant21", "composant22"]}
];

<table>
    <ng-container *ngFor="let row of rows">
        <tr *ngFor="let composant of row.composants">
            <td> {{ composant }} </td>
            <td> {{ row.nom }} </td>
        </tr>  
    <ng-container>  
</table>

关于angular - Angular 2 中的嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44631886/

相关文章:

angular - 如何将 NgControl 状态传递给 Angular 中的子组件,实现 ControlValueAccessor?

angular - 获取 "The serve command requires to be run in an Angular project, but a project definition could not be found"运行 Docker Compose

angular - Angular 5主/明细路由最佳实践

angular - 如何在 Angular2 中将可观察到的值传递给(单击)处理程序

html - Angular 4 - 动态添加的元标记不起作用

angular - 使用 ScrollStrategy 声明式创建 Overlay

angular - 如何动态翻译资源 ngx-translate

Angular 5 模块构造函数中的测试代码

angular - 如何在angular2中模拟 Protractor 中的拖放 Action ?

javascript - 在浏览器上前后保持应用过滤器 Angular 2