如何使用 ngIf 在 ngFor 回圈中設定 ngModel?
我正在使用帶有 @progress/kendo-angular-inputs 的 Angular 8.2.14
在下面的代碼中,如果option === checked,我如何使用 ngif 將 ngModel 設定為 true ?
否則,所有復選框都會被選中,而不僅僅是應該選中的復選框。
<div*ngFor="let option of contacts">
<input
style="width: auto; margin-right: 10px"
type="checkbox"
value="{{ option }}"
[(ngModel)]="checked"
kendoCheckBox
/>
<label for="{{ option }}">{{ option }}</label>
</div>
uj5u.com熱心網友回復:
為什么你首先要使用 ngIf (使用 ngIf 無論如何你都會隱藏你的復選框)。我認為您想要實作的目標是這樣的。
// in your html
<div *ngFor="let option of contacts">
<input
style="width: auto; margin-right: 10px"
type="checkbox"
[checked]="checked === option"
(change)="changeCheck(option)"
/>
<label for="{{ option }}">{{ option }}</label>
</div>
// in your ts
contacts = ['a', 'b', 'c'];
checked = 'a';
通過這種方式,您將創建 3 個復選框,并且您可以單獨檢查每個復選框。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/431911.html
