html - 在 *ngfor 复选框列表中定位一个复选框
问题描述
我有一个绑定到我的组件打字稿文件中的对象的复选框列表,我希望它在用户单击复选框时选中/取消选中列表,但由于某种原因,它只选中并取消选中第一个复选框列表,而不是用户单击的列表。下面是代码:
<div>
<ul class="reports-container">
<li *ngFor="let item of data.reports" [class.active]="selectedReport==item" >
<input type="checkbox" id="{{'savedreport'+i}}" class="k-checkbox" [checked]="item.IsSubscribed" [value]="item.IsSubscribed"(change)="onChkBoxChange($event,item)" />
</li>
</ul>
</div>
这是打字稿功能:
onChkBoxChange(event, item: SavedReport) {
item.IsSubscribed = event.target.checked;
}
当我放一个断点时,它总是在列表中的第一项中传递,有什么想法吗?
解决方案
正如@Michael Beeson 建议的那样,我在我的复选框值上使用了双向绑定,这解决了问题,所以现在的代码是:
<div>
<ul class="reports-container">
<li *ngFor="let item of data.reports" [class.active]="selectedReport==item" >
<input type="checkbox" id="{{'savedreport'+i}}" class="k-checkbox" [checked]="item.IsSubscribed" [(value)]="item.IsSubscribed"(change)="onChkBoxChange($event,item)" />
</li>
</ul>
</div>
推荐阅读
- jdbc - T4CPreparedStatement 实例未释放与绑定参数相关的内存,导致内存随时间泄漏
- java - 信号服务器错误:org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException
- r - 如何累计添加时间
- python - 深度图错误结果
- html - 如何在启动 Angular MatBottomSheet 之前加载相机数据
- python - djnago 中的“赋值前引用的局部变量‘响应’”错误
- python - 非线性决策边界的 SVM 图
- django - 我对 django-allauth linkedin 注册有疑问
- postgresql - 无法在 Ubuntu 19.04 上安装 Posgresql
- python - 如何使用 lru_cache 缓存不同类实例中使用的 utils 函数?