Angular2 TypeError:val.slice不是函数。 Primeng数据表和嵌套数据的问题

问题描述 投票:0回答:1

我正在尝试使用primeng dataTable。我在下面给出的“jsonArray”中加载嵌套的JSON:

{
  "person_name": "Gaurav",
  "personal_details":[{
    "last_name":"patel",
    "age":24
  }],
  "technical_details":[{
    "roll_no":1,
    "branch":"IT"
  }]
}

//app.component.ts
import { Component } from '@angular/core';
import { AppService } from './app.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  jsonArray: any[]=[];
  constructor(public AppSVC: AppService) { }

  ngOnInit() {

    this.AppSVC.getAppontmentJson().subscribe(res => {
      if (!res.error_status) {
        this.jsonArray = res;
      }
    });
  }
}
//app.component.html
<p-dataTable [value]="jsonArray" reorderableColumns="true">
  <p-column header="Last Name" field="personal_details.last_name"></p-column>
  <p-column header="Age" field="personal_details.last_name"></p-column>
</p-dataTable>

但是当我运行代码时,它显示一个错误:

错误TypeError:val.slice不是函数

我是棱角分明2的新手

angular
1个回答
1
投票

好像数据格式不正确。您需要为数据表分配一个数组。

[{
  "person_name": "Gaurav",
  "personal_details":{
    "last_name":"patel",
    "age":24
  },
  "technical_details":{
    "roll_no":1,
    "branch":"IT"
  }
}]
© www.soinside.com 2019 - 2024. All rights reserved.