类型'PTOData []'的参数不能分配给'PTOData'类型的参数

时间:2017-06-20 17:13:59

标签: angular

在我的角度项目中,我收到标题中所述的错误,其解释为"Property 'ID' is missing in type 'PTOData[]'."但是,每当我在其末尾添加.ID时,它就会显示{{ 1}}它肯定会。我在某处做错了吗?

这是我的.ts文件:

"Property 'ID' does not exist on type 'PTOData[]'."

这是我的.html文件:

import { Component, OnInit, Input } from '@angular/core';

import { PTOData } from './pto-data';
import { PTODataService } from './pto-data.service';
import { Location } from '@angular/common';

@Component({
    selector: '[pto-row-edit]',
    templateUrl: `./row-edit.component.html`,
    styleUrls: ['./row-edit.component.css']
})

export class RowEditComponent {
    @Input() pto: PTOData[];

    constructor(
        private ptodataservice: PTODataService,
        private location: Location) { }

    saveRow(): void {
        this.ptodataservice.update(this.pto.ID)
    }
}

这也是我的更新功能

<td><input class='form-control' type="date" id="ptoDate" [ngModel]="pto.date | date: 'yyyy-MM-dd'" (ngModelChange)="pto.date=$event" name="ptoDate" /></td>
<td>
  <select class="form-control" id="ptoFullHalf" [(ngModel)]="pto.fullhalf" name="ptoFullHalf">
    <option value="full">Full</option>
    <option value="AM">AM</option>
    <option value="PM">PM</option>
    <option value="(full)">(Full)</option>
    <option value="(half)">(Half)</option>
  </select>
</td>
<td>
  <select class="form-control" id="ptoHours" [(ngModel)]="pto.hours" name="ptoHours">
    <option value="4">4</option>
    <option value="8">8</option>
    <option value="-4">-4</option>
    <option value="-8">-8</option>
  </select>
</td>
<td>
  <select class="form-control" id="ptoScheduled" [(ngModel)]="pto.scheduled" name="ptoScheduled">
    <option value=""></option>
    <option value="advanced">Advanced</option>
    <option value="scheduled">Scheduled</option>
    <option value="unscheduled">Unscheduled</option>
  </select>
</td>
<td><input class='form-control' type="text" id="ptoNotes" [(ngModel)]="pto.notes" name="ptoNotes" /></td>
<td>
  <input class="form-check-input" type="checkbox" id="ptoinPR" [(ngModel)]="pto.inPR" name="ptoinPR" />
</td>
<td>
  <button class="btn btn-default btn-primary" style="float:right;"><i class="fa fa-ban" aria-hidden="true"></i></button>
  <button class="btn btn-default btn-primary" style="float:right;" (click)="saveRow()"><i class="fa fa-check" aria-hidden="true"></i></button>
</td>

1 个答案:

答案 0 :(得分:2)

应该是 object 而不是 array

export class RowEditComponent {
    @Input() pto: PTOData;

或者你只需​​要传递特定对象的id,传递索引

saveRow(): void {
    this.ptodataservice.update(this.pto[0].ID)
}

修改

根据您的服务,您需要传递完整的对象而非ID,

尝试传递this.pto

saveRow(): void {
    this.ptodataservice.update(this.pto)
}