簡體   English   中英

如何在Angular中的JSON數據上使用* ngFor?

[英]How to use *ngFor on a JSON data in Angular?

我試圖創建一個動態表,將行和列定義為數組和JSON對象。 我能夠動態建立列標題。 但是這些行沒有被渲染。 我來自angularjs 1.x背景,對angular還是陌生的。

請幫助我們了解問題。 這是the人https://plnkr.co/edit/BG22Z1videTlzU3SrjcT?p=preview

<table class="table">  
            <thead> 
                <tr> 
                    <th *ngFor="let column of columnNames">
                        {{column}}
                    </th>                       
                </tr> 
            </thead> 
            <tbody> 
                <tr *ngFor="let userDate of userDateObj">                       
                    <td>{{userDate.name}}</td> 
                    <td>{{patientData.dob}}</td> 
                </tr>                         
            </tbody> 
        </table> 

我觀察到在您的監聽器中userDateObj不是數組。 您需要將其更正為

<tr *ngFor="let userDate of userDateObj.userDate">

我已經編輯了插件,請在這里找到更新

要使用* ng,該對象必須是一個數組。 如果不是數組,則可以使用管道將其轉換為數組。

暫無
暫無

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

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