Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2 TypeError:val.slice不是函数。初始化数据表和嵌套数据时出现问题_Angular_Primeng_Primeng Datatable - Fatal编程技术网

Angular2 TypeError:val.slice不是函数。初始化数据表和嵌套数据时出现问题

Angular2 TypeError:val.slice不是函数。初始化数据表和嵌套数据时出现问题,angular,primeng,primeng-datatable,Angular,Primeng,Primeng Datatable,下面是我的json的结构: [ { "id": 1, "race_location": "Charlotte-Fall", "year": null, "mec_date": null, "nxs_date": null, "ctws_date": null, "event_details": { "titans": { "required": 5, "drivers": [

下面是我的json的结构:

[
{
    "id": 1,
    "race_location": "Charlotte-Fall",
    "year": null,
    "mec_date": null,
    "nxs_date": null,
    "ctws_date": null,
    "event_details": {
        "titans": {
            "required": 5,
            "drivers": [
                "Andrew James Fisher"
            ],
            "trailers": [
                "T-8"
            ],
            "count": 1,
            "vehicles": [
                "C-16"
            ]
        },
        "coaches": {
            "required": 3,
            "drivers": [],
            "trailers": [],
            "count": 0,
            "vehicles": []
        },
        "nxs": {
            "required": 4,
            "drivers": [
                "Todd Michael Bixby",
                "David Alan Vestal",
                "Mark A Hadley",
                "Bryan L Kleinsasser"
            ],
            "trailers": [
                "T-24",
                "T-35",
                "AT-4",
                "T-29"
            ],
            "count": 4,
            "vehicles": [
                "C-38",
                "C-30",
                "C-21",
                "C-37"
            ]
        },
        "wheels": {
            "required": 4,
            "drivers": [],
            "trailers": [],
            "count": 0,
            "vehicles": []
        },
        "cups": {
            "required": 13,
            "drivers": [
                "David Andrew Charest"
            ],
            "trailers": [
                "T-4"
            ],
            "count": 1,
            "vehicles": [
                "C-48"
            ]
        },
        "tvs": {
            "required": 2,
            "drivers": [
                "Robin Eugene Angle",
                "Christopher Alan Beach"
            ],
            "trailers": [
                "F-6",
                null
            ],
            "count": 2,
            "vehicles": [
                "C-40",
                "C-44"
            ]
        }
    }
},
{...
}
]
组件1.ts:

export class RacescheduleComponent implements OnInit {
  races: any[]=[];
  selectedRace: null;
  errorMessage: string;

  public constructor(private eventService: EventService, private 
router: Router) { }

  ngOnInit() {
    this.eventService.getList('race_schedule')
   .subscribe(races => {
    var races_ = [];
    this.races = races.map((race) => {
      var progress = 0;

      if (race.event_details.cups.count >= race.event_details.cups.required) {
        progress += 50 ;
      }
      if (race.event_details.nxs.count >= race.event_details.nxs.required) {
        progress += 50 ;
      }

      race.total_progress = progress;
      return race;
    });
  }, error => this.errorMessage = <any>error);
  }

selectRace(e) {
this.selectedRace = e.data;
}
}
如果我在下面使用一个常规的html表,它就可以正常工作

 <div class="text-left"><h4><i class="fa fa-truck fa-2x"></i> Cup Box 
 Trailers</h4></div>
  <table class="table table-striped">
    <thead>
    <tr class="info">
      <th>Driver</th>
      <th>Tractor</th>
      <th>Trailer</th>
    </tr>
    </thead>
    <tbody>
    <tr *ngFor="let driver of selectedRace.event_details.cups.drivers; 
    index as i">
      <td>{{ driver }}</td>
      <td>{{ selectedRace.event_details.cups.vehicles[i] }}</td>
      <td>{{ selectedRace.event_details.cups.trailers[i] }}</td>
    </tr>
    </tbody>
  </table>
杯形盒
拖车
司机
拖拉机
拖车
{{driver}}
{{selectedRace.event_details.cups.vehicles[i]}
{{selectedRace.event_details.cups.trailes[i]}

我对嵌套结构使用datatable的方式是否错误?如何在html中正确定义第二个datatable?非常感谢您的任何输入。

我的问题是最初没有将数据表绑定到数组。我认为你的问题与所选的比赛是一样的。您正在将其初始化为null,而不是数组

这是我愚蠢的错误:

ngOnInit() {
this.lotteries = <ILottery[]>{};
...
应该是这样的:

races: any[]=[];
selectedRace: any[]=[];
ngOnInit() {
this.lotteries = <ILottery[]>{};
...
ngOnInit() {
this.lotteries = <ILottery[]>[];
...
races: any[]=[];
selectedRace: null;
races: any[]=[];
selectedRace: any[]=[];