html - 如何在 Angular 中的复选框上设置默认值?
问题描述
我有以下代码:
<input type="checkbox" [(ngModel)]="i.checkt" [ngModelOptions]=
{standalone:true} (change)="recovery(i.checkt,i.TherapeuticArea)">
{{i.TherapeuticArea}}
我面临的问题是,使用standalone:true 默认选中每个复选框,当standalone 为false 时,复选框不起作用。有没有办法在为用户提供完整功能的同时将复选框值设置为未选中?
解决方案
您需要checked
在输入上设置属性,如下所示:
<input type="checkbox" [(ngModel)]="i.checkt" [ngModelOptions]={standalone:true}
(change)="recovery(i.checkt,i.TherapeuticArea)" [checked]="i.checkt">
但我会推荐@Florian 的评论,即使用 FormControl 来管理来自 UI 的输入。在我看来,它将为您节省大量时间并使其更易于维护。
推荐阅读
- reactjs - 如何用酶为材料 ui Radio 编写测试
- sql - SQL 仅选择最后一行的用户名
- flutter - Riverpod - 合并提供商时出现问题
- typescript - 如何将区分联合转换为 DU,其中每个案例都是初始 DU 案例的数组
- html - 在移动设备中将滚动方向修改为水平
- java - 找不到适合 jdbc:mysql://localhost::3306/gestionpersonnel?serverTimezone=UTC 的驱动程序
- ios - 使用 SwiftUI 的 ReplayKit 广播上传扩展
- javascript - 如何每隔一秒调用一次函数异步等待?
- javascript - 如何使用 Async/await 将获取的 JSON 数据正确显示到 DOM
- html - 确保 figcaption 宽度永远不会超过图像宽度