我可以在HTML中两次使用* ngFor吗?

时间:2019-06-17 21:11:59

标签: javascript html angular typescript

enter image description here

我正在获取一些数据,并使用*ngFor将其显示在表中。但是我也有一个数组,需要遍历并在同一表中显示,但不能使用{{1} }再次针对此新数组,因为它们也将彼此循环。

*ngFor

我尝试过,但是没有用。我也尝试过<tbody *ngFor="let update of updates"> <tr *ngFor="let product of products"> <td>{{ product.id }}</td> <td>{{ product.name }}</td> <td>{{ product.currentPrice }}</td> <td> {{ update }} </td> </tr> </tbody> ,但也不起作用。

一些额外的信息:更新是timeStamp的转换版本。我正在获取的JSON具有毫秒级的时间戳,因此我将其转换为正常日期并尝试将其显示回表中。

one is with the update and the other one with the products working well

这是我的JSON:https://api.myjson.com/bins/1ao4vx

和更新数组:

*ngFor="let product of products, let update of updates"

1 个答案:

答案 0 :(得分:-1)

我不认为您想要double for循环。从您的图表中假设产品和更新的长度相等...

<ng-container *ngFor="let update of updates; let i = index"> 
    <tbody>
            <tr>
                 <td>{{ products[i].id }}</td>
                 <td>{{ products[i].name }}</td>
                 <td>{{ products[i].currentPrice }}</td>
                 <td> {{ update }} </td>
            </tr>
    </tbody>
</ng-container>