【发布时间】:2020-11-30 21:21:17
【问题描述】:
我需要帮助。 我有一个车辆清单。每辆车都有一个值“类型”,无论是汽车还是摩托车。我想使用复选框(汽车和摩托车)过滤此列表。我不确定这里的最佳做法是什么。
filter.component.html
<div class="form-group">
<form class="form-group" [formGroup]="vehiclesFormGroup">
<label class="custom-control custom-checkbox" >
<input type="checkbox" class="custom-control-input" formControlName="car" [(ngModel)]="car"/>
<span class="custom-control-label">Car</span>
</label>
<label class="custom-control custom-checkbox" >
<input type="checkbox" class="custom-control-input" formControlName="motorcycle" [(ngModel)]="motorcycle"/>
<span class="custom-control-label">Motorcycle</span>
</label>
<button class="btn btn-primary btn-block position-sticky btn-search" type="button" (click)="filterVehicles()">Filter Vehicles</button>
</form>
</div>
filter.component.ts
car: false;
motorcycle: false;
vehiclesFormGroup = this.formBuilder.group({
car: this.car,
motorcycle: this.motorcycle,
})
filterVehicles() {
console.log('car:', this.car)
console.log('motorcycle:', this.motorcycle)
}
如果选中复选框,则控制台输出为“true”,如果未选中,则为未定义。我想我需要使用vehicle.type === 'car' 和vehicle.type === 'motorcycle' 或其他东西来过滤它。你有例子吗?
【问题讨论】:
-
在这里我回答了相同的解决方案:stackoverflow.com/a/63217990/8495145 和 Blitz 链接 stackblitz.com/edit/angular-form-filtering?file=src/app/…
标签: angular forms checkbox filter