【发布时间】:2020-08-23 04:10:52
【问题描述】:
我有一个带有这些复选框的表单,以允许用户选择一个项目的多个“口径”:
这些复选框是通过 ngFor 从一个名为 'calibres' 的数组中创建的,该数组具有所有可能的值,如下面的代码所示:
component.html
<div >
<mat-checkbox #checkBox
*ngFor="let calibre of calibres; let i = index"
[value]="calibre"
(change)="getCheckbox()"
class="example-margin mb-0 mt-1" >{{calibre}}</mat-checkbox>
</div>
component.ts 上的 getCheckbox() 函数创建一个包含在我的复选框中选中的元素的数组
getCheckbox() {
this.item.calibres = [];
const checked = this.checkBox.filter(checkbox => checkbox.checked);
checked.forEach(data => {
this.item.calibres.push ( data.value );
});
}
当我提交表单时,这个已检查元素的数组存储在后端中,用于表单创建的这个特定“项目”,因此存储的数组将类似于 [50,60]。仅选中复选框。
我要做的是在填写表单时(出于“编辑”项目的目的)检查存储在数组中的那些复选框。
我怎样才能做到这一点?
【问题讨论】:
标签: javascript arrays angular checkbox