【发布时间】:2018-01-26 14:51:09
【问题描述】:
我有一个 angular 2 反应形式,其中包含数据绑定的复选框列表。这工作正常,但现在我需要添加一个按钮来检查所有复选框。
以下是我的 FormGroup 的配置方式:
private buildForm() {
this.groupForm = this._formBuilder.group({
bookId: this._formBuilder.control(null),
startDate: this._formBuilder.control(null),
groupTotal: this._formBuilder.control(null),
chapterGrouping: this._formBuilder.control("all"),
groupChapters: this.buildChapters()
});
}
private buildChapters() {
const chapters = this.chapters.map(chapter => {
return this._formBuilder.control(chapter.selected)
});
return this._formBuilder.array(chapters);
}
这是我的 HTML:
<div class="form-group">
<label for="">Select chapters</label>
<div *ngFor="let chapter of formChapters.controls; let i=index" class="checkbox">
<label>
<input type="checkbox" [formControl]="chapter" >
{{chapters[i].title}}
</label>
</div>
</div>
如何访问 FormArray 以将所有复选框设置为已选中?
【问题讨论】: