角度2 ngPrime表呈现问题

时间:2018-05-01 15:05:50

标签: angular

我在我的组件中自编了json,我无法使用ngPrime在模板中渲染。我尝试过类似的问题。我收到错误如下

错误错误:无法找到不同的支持对象' [object Object]'类型'对象'。 NgFor仅支持绑定到Iterables,例如Arrays。



// component.ts

export class AppComponent implements OnInit {
  title = 'app';
  sales: any = [
    { brand: 'Apple', lastYearSale: '51%', thisYearSale: '40%', lastYearProfit: '$54,406.00', thisYearProfit: '$43,342' },
    { brand: 'Samsung', lastYearSale: '83%', thisYearSale: '96%', lastYearProfit: '$423,132', thisYearProfit: '$312,122' },
    { brand: 'Microsoft', lastYearSale: '38%', thisYearSale: '5%', lastYearProfit: '$12,321', thisYearProfit: '$8,500' },
    { brand: 'Philips', lastYearSale: '49%', thisYearSale: '22%', lastYearProfit: '$745,232', thisYearProfit: '$650,323,' },
    { brand: 'Song', lastYearSale: '17%', thisYearSale: '79%', lastYearProfit: '$643,242', thisYearProfit: '500,332' },
    { brand: 'LG', lastYearSale: '52%', thisYearSale: ' 65%', lastYearProfit: '$421,132', thisYearProfit: '$150,005' },
    { brand: 'Sharp', lastYearSale: '82%', thisYearSale: '12%', lastYearProfit: '$131,211', thisYearProfit: '$100,214' },
    { brand: 'Panasonic', lastYearSale: '44%', thisYearSale: '45%', lastYearProfit: '$66,442', thisYearProfit: '$53,322' },
    { brand: 'HTC', lastYearSale: '90%', thisYearSale: '56%', lastYearProfit: '$765,442', thisYearProfit: '$296,232' },
    { brand: 'Toshiba', lastYearSale: '75%', thisYearSale: '54%', lastYearProfit: '$21,212', thisYearProfit: '$12,533' }
];

ngOnInit() {
}

<p-table [value]="sales">
  <ng-template pTemplate="body" let-data>
    <tr *ngFor="let row of data">
      <td>{{row.brand}}</td>
    </tr>
  </ng-template>
</p-table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在p表[value] =“ sales”中,“ sales”是对象数组。默认情况下,p-table将执行循环。因此,在ng-template中,“ let-data”(数据是一个对象)将一一执行对象数组。因此,我们不需要循环单个对象。您示例的正确代码是

<p-table [value]="sales">
        <ng-template pTemplate="body" let-data>
            <tr>
                <td>{{data.brand}}</td>
            </tr>
        </ng-template>
    </p-table>