【发布时间】:2018-01-18 10:04:47
【问题描述】:
我正在处理一个表单并且我正在使用 Angular Reactive Forms。 我需要在我的表单中进行多选,我确实通过按 ctrl 并选择多个选项来完成它。 我希望能够在不按 ctrl 键的情况下使用多选。 有可能吗?
表单组件:
this.productForm = new FormGroup({
'items': new FormControl(this.product.items, Validators.required),
});
模板:
<div class="form-group">
<select multiple="multiple" class="form-control" id="items" formControlName="items" required>
<option *ngFor="let item of itemList" [value]="item.id">{{item.name}}</option>
</select>
</div>
更新:
我尝试了 greg95000 的解决方案,但它只是部分可行的解决方案。
<div class="form-group">
<select multiple="multiple" class="form-control" id="items" formControlName="items" required>
<option (mousedown)="onMouseDown($event)" (mousemove)="$event.preventDefault()" *ngFor="let item of items" [value]="item.id">{{item.name}}</option>
</select>
</div>
public onMouseDown(event: MouseEvent) {
event.preventDefault();
event.target['selected'] = !event.target['selected'];
}
此解决方案打破了数据绑定
【问题讨论】:
标签: angular multi-select angular-reactive-forms