【问题标题】:How do I set a default value on a checkbox in Angular?如何在 Angular 中的复选框上设置默认值?
【发布时间】:2019-05-02 10:54:14
【问题描述】:

我有以下代码:

<input type="checkbox" [(ngModel)]="i.checkt" [ngModelOptions]= 
{standalone:true} (change)="recovery(i.checkt,i.TherapeuticArea)"> 
{{i.TherapeuticArea}}

我面临的问题是,使用standalone:true 默认选中每个复选框,当standalone 为false 时,复选框不起作用。有没有办法在为用户提供完整功能的同时将复选框值设置为未选中?

【问题讨论】:

标签: html angular checkbox


【解决方案1】:

您需要在输入上设置checked 属性,如下所示:

<input type="checkbox" [(ngModel)]="i.checkt" [ngModelOptions]={standalone:true} 
(change)="recovery(i.checkt,i.TherapeuticArea)" [checked]="i.checkt"> 

但我会推荐@Florian 关于使用 FormControl 管理来自 UI 的输入的评论。在我看来,它将为您节省大量时间并使其更易于维护。

【讨论】:

    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 2015-11-17
    • 1970-01-01
    • 2021-02-18
    相关资源
    最近更新 更多