angular - 角度复选框仅保存已更改的复选框
问题描述
我有一个使用以下方法构建的复选框列表:
<div *ngFor="let rule of rules">
<div class="form-check">
<input type="checkbox" [(ngModel)]="rule.isActive" name="{{rule.ruleId}}" id="{{rule.ruleId}}" class="form-check-input">
<label class="form-check-label"> {{rule.description}} - {{rule.message}}</label>
</div>
</div>
所有这些都在以下表格中:
<form #ruleForm="ngForm" (ngSubmit)="updateRule(ruleForm.value)" novalidate>
在 updateRule 方法中是否有知道哪些复选框已更改?我必须进行保存,但与其全部保存,我只想保存已更改的那些。
解决方案
您可以跟踪检查的规则 id 您可以通过以下方式进行操作:
组件.html
<div *ngFor="let rule of rules">
<div class="form-check">
<input type="checkbox" (change)="onCheck($event,rule.ruleId)" [checked]="rule.isActive" value="{{rule.ruleId}}" name="{{rule.ruleId}}" id="{{rule.ruleId}}" class="form-check-input">
<label class="form-check-label"> {{rule.description}} - {{rule.message}}</label>
</div>
</div>
组件.ts
activeRulesIds= []; // declare and initialize'
...........
...........
...........
onCheck(event, $value) {
if (event.target.checked) {
this.activeRulesIds.push($value);
}
else {
this.activeRulesIds.splice(this.activeRulesIds.indexOf($value), 1);
}
}
推荐阅读
- python - 指定从 Discord 在 Google 表格中写入数据时要使用的工作表名称
- python - 如何根据输入进行while循环?
- excel - 如何使用 VBA 检查是否在 Excel 中应用了特定过滤器?
- ios - 如何取消 AWS Amplify Auth 操作,例如 SignIn、SignUp
- python - 通过 boto3 解析 IAM 策略文档响应
- python - 使用 Python 并发来降低效率是否合理?
- c++ - 创建一个自定义 lightdm 迎宾器
- python-3.x - 交叉检查我的代码(使用 Raspberry pi 的运动检测器)
- django - 在 Django 中创建一个动态表单(不仅在项目数量上,而且在结构上)
- swiftui - 如何获得 LinearGradient 的原始渐变颜色?