我有以下代碼角2多選擇框活性形式只返回一個值檢查
Template:
<div [formGroup]="booksFormGroup" class="btn-group" data-toggle="buttons">
<ng-container *ngFor="let book of books">
<label class="btn" [ngClass]="{'active': book.Id === selectedBookId}"
(click)="bookClicked(book.Id)">
<input type="checkbox" class="toggle" [value]="book.Id" formControlName="buttonsBook">{{book.Name}}
</label>
</ng-container>
</div>
<button (click)=onClicked()>Click</button>
TS file:
this.buttonsBook = new FormControl();
this.booksFormGroup = new FormGroup({
buttonsBook: this.buttonsBook,
});
onClicked(){
var selected = this.booksFormGroup.value.buttonsBook
}
當我點擊按鈕,我只得到了最後一個複選框,我查了一下,即使超過一個的值選擇。 我需要獲取所有選中的複選框值。
我看到了一些解決方案,但它們不是基於反應形式,而是基於模板和反應的混合,我不太喜歡。
我沒有看到一個
@AirirDhaDas formGroup不能以表格標籤 – yurzui
[Angular如何獲得多個複選框值?](https://stackoverflow.com/questions/43423333/angular-how-to-get-the-multiple -checkbox-value) –