Nested loops in angular 2

后端 未结 3 1651
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 23:53

I want to display the data of this array:

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


        
相关标签:
3条回答
  • 2020-12-12 00:13

    Try this:

    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>
    
    0 讨论(0)
  • 2020-12-12 00:26

    I use a bit of a different syntax that got the job done

    http://plnkr.co/edit/Z94BxYiu2UTU7h3oySui?p=preview

    <table>
    <tr>
    <th>Composant</th>
    <th>Nom</th>
    </tr>
    <template ngFor #item [ngForOf]="array">
      <template ngFor #noo [ngForOf]="item.composants">
        <tr>
          <td>{{noo}}</td>
          <td>{{item.nom}}</td>
        </tr>
        </template>
    </template>
    </table>
    
    0 讨论(0)
  • 2020-12-12 00:34

    Change code like below:

        <table>
    <tr><th>Composant</th> 
    <th>Nom</th>
    </tr>
    <template *ngFor="let item of array">
    <template *ngFor="let composant of item.composants">
     <tr> <td>{{item.nom}}</td> <td>{{composant|json}}</td> 
    </tr>
    </template> 
    </template> 
    </table>
    
    0 讨论(0)
提交回复
热议问题