angular - 取消选中所有选中的复选框 Angular2

我有一个从 ngFor 循环和一个重置按钮生成的复选框列表。单击重置按钮时,如果选中了任何复选框,我想取消选中它们。

这是我的html

<li *ngFor="let item of listItems" class="filter-list__item">
    <label class="filter-list__itemLabel">
        <input type="checkbox" value="{{item.id}}" [checked]="checked" />
        <span innerHTML="{{item.name}}"></span>
    </label>
</li>
<button (click)="resetAll()"></button>

最佳答案

您可能想像这样将 checked 属性绑定(bind)到项目的状态。

<li *ngFor="let item of listItems" class="filter-list__item">
    <label class="filter-list__itemLabel">
        <input type="checkbox" value="{{item.id}}" [checked]="item.checked" />
        <span innerHTML="{{item.name}}"></span>
    </label>
</li>

<button (click)="resetAll()"></button>

现在您可以在组件中创建 resetAll 函数来访问列表项并将值重置为 false

resetAll() {
  this.listItems.forEach((item) => {
    item.checked = false;
  })

https://stackoverflow.com/questions/40172846/

相关文章:

json - R + fromJSON - 如何发送标题信息?

c# - 如何从 MVC razor c# 中的动态模型获取属性值

cuda - 从未对齐的 uint8_t 重铸为 uint32_t 数组读取 - 未获取所有值

xcode - 我找不到 podfile

vb.net - 为什么异步函数返回 System.Threading.Tasks.Task`1[S

php - 基于其他数组排序数组

apache-spark - 从 Spark 转换函数中动态地从 HDFS 读取文件

php - 将变量从按钮传递到 Controller Laravel

indexing - 确定 Teradata 中表的主索引

Python/Django - 需要一个字符串或类似字节的对象