我对Angular很陌生。我在Angular 7中使用的版本。
因此,我有cars.component.ts
中的汽车列表,这些汽车正在使用service
从JSON文件中提取。列表中的每辆车都有一个编辑和删除选项,我还有一个添加新车的按钮。
编辑选项有一个更新选项,当我按下该选项时,出现错误ERROR TypeError: Cannot set property 'name' of undefined
。我错过了一些东西,但不确定到底是什么。
下面是代码。
cars.component.ts
import { Component, OnInit } from '@angular/core';
import { CarService } from '../service/car.service';
import { ICars } from '../cars';
@Component({
selector: 'app-cars',
templateUrl: './cars.component.html',
styleUrls: ['./cars.component.css']
})
export class CarsComponent implements OnInit {
public cars = [];
registeredCars = [];
selectedRow: number;
public car: ICars;
showNew: Boolean = false;
submitType: string;
loading: Boolean = false;
constructor(private _carService: CarService) { }
ngOnInit() {
this.loading = true;
//console.log('loading', this.loading);
this._carService.fetchData().subscribe(data => this.cars = data);
}
onEdit(index: number) {
this.selectedRow = index;
this.car = new ICars;
this.car = Object.assign({}, this.cars[this.selectedRow]);
this.showNew = true;
this.submitType = 'Update';
}
onDelete(index: number) {
this.cars.splice(index, 1);
}
onNew() {
this.car = new ICars;
this.submitType = 'Save';
this.showNew = true;
}
onSave(index: number) {
this.selectedRow = index;
if (this.submitType === 'Save' ) {
this.cars.push(this.car);
} else {
console.log('this car', this.car.name);
this.car[this.selectedRow].name = this.car.name;
this.car[this.selectedRow].year = this.car.year;
}
this.showNew = false;
}
onCancel() {
this.showNew = false;
}
}
这是部分代码
<div class="carsList">
<table *ngIf="loading" class="table table-striped">
<thead >
<tr>
<th>#</th>
<th>Name</th>
<th>Year</th>
</tr>
</thead>
<tbody >
<tr *ngFor="let car of cars; let i = index">
<td>{{ i+1 }}</td>
<td>{{car.name | uppercase}}</td>
<td>{{car.year}}</td>
<td>
<button type="button" class="btn btn-info" routerLink="/car-details/{{ i }}" placement="top" ngbTooltip="View details"><fa name='eye'></fa></button>
</td>
<td>
<button type="button" class="btn btn-secondary" (click)="onEdit(i)" placement="top" ngbTooltip="Edit details"><fa name='edit'></fa></button>
</td>
<td>
<button type="button" class="btn btn-danger" (click)="onDelete(i)" placement="top" ngbTooltip="Delete entry"><fa name='trash'></fa></button>
</td>
</tr>
</tbody>
</table>
<div class="text-right">
<button type="submit" class="btn btn-primary" (click)="onNew()">New</button>
</div>
</div>
<!-- Edit/Add User -->
<div class="regentry" *ngIf="showNew">
<h2 class="text-center">{{ submitType === 'Save'? 'Register New Car' : 'Edit Car' }}</h2>
<br>
<form (ngSubmit)="onSave()">
<div class="form-group row">
<label for="name-input" class="col-2 col-form-label">Car Name</label>
<div class="col-10">
<input class="form-control" type="text" [(ngModel)]="car.name" name="name" required>
</div>
</div>
<div class="form-group row">
<label for="year-input" class="col-2 col-form-label">Year</label>
<div class="col-10">
<input class="form-control" type="text" [(ngModel)]="car.year" name="year" required>
</div>
</div>
<button type="submit" class="btn btn-success">{{submitType}}</button>
<button type="submit" class="btn btn-primary" (click)="onCancel()">Cancel</button>
</form>
</div>
<div *ngIf="!loading"><img class="loading" src="../../assets/loading.gif" alt="loading" srcset=""></div>
答案 0 :(得分:2)
您需要将要保存的索引传递到模板中的onSave函数:onSave(index)
但是,由于您已经在onEdit函数中设置了selectedRow,因此只需从onSave中完全删除参数,然后删除行this.selectedRow = index
。这会更简单。
因为您没有传递参数,所以index
是undefined
,然后在使用它在this.selectedRow
上查找值之前,将该不想要的值分配给this.car
删除我提到的行后,应在单击保存时将this.selectedRow
保留为有效的索引号。