【发布时间】:2019-07-05 06:16:00
【问题描述】:
我想在 Angular 上使用 ngFor 显示一个数组(我正在使用 Angular 6)。我希望能够通过单击复选框来选择我的一些汽车 最后,当我按下按钮时,我会购买选定的汽车。
列表显示正常,复选框也显示,但我真的不知道如何轻松检索选定的汽车。 你能帮我吗?我已经尝试了一些我在 StackoverFlow 上找到的示例,但没有成功
我不想触摸只包含 3 个字段(id 品牌和价格)的车型
<thead>
<button type="button" click="buySelectedCars()">BUY</button>
</tr>
<th>car id</th>
<th>car brand</th>
<th>car price</th>
<th> checkbox</th>
<tr>
</thead>
<tbody>
<tr *ngFor="let car of cars">
<td>{{car.id}}</td>
<td>{{car.brand}}</td>
<td>{{car.price}}</td>
<td>
<input type="checkbox" [checked]='car.check'></td>
</td>
</tr>
</tbody>
TS:
buySelectedCars(){
for (car of cars){
if car.check{
this.carService.buyCar(car);
}
}
}
【问题讨论】: