簡體   English   中英

Angular2多個ngFor用於單行

[英]Angular2 multiple ngFor for single row

我試圖顯示帶有值的表。 該表有5列,在第3列中,我需要另一個表的值。 我該如何實施?

我的html標記是:

list.component.html

<table >
            <thead>
                <tr>
                    <th> first </th>
                    <th> second </th>
                    <th> Third </th>
                    <th> Fourth </th>
                    <th> fifth </th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let item of items">

                    <td> {{item.name}}</td>
                    <td> {{item.subject}}</td>
                    <td *ngFor="let list of details">
                        {{list.firstName}}{{list.lastName}} ----> problem is here  next 2 tds are not displaying the values properly
                    </td>
                    <td> {{item.fisrt}}</td>
                    <td> {{item.second}}</td>
                </tr>
            </tbody>
        </table>

我的物品陣列

items[{name:"", subject:"", first:"", second:""}, {name:"", subject:"", first:"", second:""}]

我的清單陣列

  list[{firstName:"abc", lastname:"pqr"}{firstName:"abc", lastname:"pqr"}]

前兩列中的值來自一個表,第三列中的值來自另一表; 其余兩列再次具有來自第一個表的數據。

我應該如何實施呢?

我猜您在這種情況下需要嵌套表:

<tr *ngFor="let item of items">
    <td> {{item.name}}</td>
    <td> {{item.subject}}</td>
    <td>
      <table>
         <tr>
           <td *ngFor="let list of details">{{list.firstName}} {{list.lastName}}</td>
         </tr>
      </table>
    </td>
    <td> {{item.fisrt}}</td>
    <td> {{item.second}}</td>
</tr>

我遇到了您面臨的問題。 我已經創建了一個演示@ plnkr

問題是您在items數組中有兩個對象,因此每個對象循環兩次。

暫無
暫無

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

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