angular - 如何使用angular8在反应形式中使复选框值为真
问题描述
我已经动态绑定了复选框值。所以现在基于检查和取消检查,我需要传递相同的数据数组,值为真/假。但是现在,即使检查了值,它也只显示假,并且基于检查和取消检查值,如果我取消检查复选框中的任何一个,选择/取消选择仍处于选中状态。
演示:演示
TS:
checkAll(ev) {
if (!this.all) {
this.PrintList.forEach(x => x.value = true)
this.isAllChecked()
} else {
this.PrintList.forEach(x => x.value = false)
this.isAllChecked()
}
}
isAllChecked() {
this.all = !this.all
}
onChange(event, item) {
if(event){
this.printLists.push(item);
} else {
this.printLists.splice(this.printLists.indexOf(item), 1)
}
}
解决方案
如果您想在没有反应形式的情况下执行此操作,这是一种选择。离开了,在基于的迭代中printLists
赋值。您可以为全选/取消全选复选框使用 getter,我们也在函数中使用它。因此,基于此,我提出以下建议:print.value
$event.target.checked
checkAll
在迭代中:
<input type="checkbox" [checked]="print.value" (change)="print.value = $event.target.checked" ...>
全选/取消全选复选框:
<input type="checkbox" [checked]="isAll" (change)="checkAll()">
然后是checkAll()
函数和getter isAll
:
checkAll(ev) {
// check if all are checked or not and act accordingly
if (!this.isAll) {
this.PrintList.forEach(x => (x.value = true));
} else {
this.PrintList.forEach(x => (x.value = false));
}
}
get isAll() {
// return true/false based on if all are checked or not
return this.PrintList.every(x => x.value === true);
}
你的分叉STACKBLITZ
推荐阅读
- node.js - 无法从服务器重定向到客户端 URL
- kotlin - 如果在带有类型参数的接口上定义了方法,则 kotlinFunction 返回 null
- mysql - 如何将两个查询的结果组合成一个数据集(一个在另一个之上)并且仍然显示下表的标题
- android - 使用 Intent 启动一个新的 Fragment
- html - CSS box-sizing:border-box 仍然为元素宽度添加边框大小
- node.js - 是否可以从 Express 中的路由调用我的减速器中的操作函数?
- flutter - 如何解析类型“时间戳”不是类型转换中“字符串”类型的子类型
- c# - 如何从 Dotnet Core 中的字符串向 Mongodb 插入数据
- mysql - MySQL 错误:无法更新表 SQL 触发器
- nginx - 无法使用 NGINX 提供图片