嗨,我有一个表,我想要更改表中的元素。
这是我的代码。我想改变汽车的颜色然后更新它。如果我按照我的代码,car.color保持不变。我试图在输入类型中使用Ngmodel,但它会更改我列表中的所有元素。
我能做什么 ?
<tr *ngFor="let car of cars">
<td><input type="text value="{{car.color}}"></td>
<td>{{ car.brand }}</td>
<td>{{ car.id }}</td>
<td"><button type="button" (click)="onUpdateCar(car)"> update </button></td>
</tr>
编辑:
这里应该是ts文件,希望发送我的汽车对象的更新版本
onUpdateCar(car) {
this.carService.updateCar(car).subscribe();
}
您实际上并不需要文本框中的value属性,而是可以使用ngModel
如下: -
<tr *ngFor="let car of cars">
<td><input type="text [(ngModel)]="car.color"></td>
<td>{{ car.brand }}</td>
<td>{{ car.id }}</td>
<td"><button type="button" (click)="onUpdateCar(car)">Update</button></td>
</tr>