简体   繁体   English

未触摸时将 Checkbox 的默认返回值设置为 false

[英]Set default return Value to false of Checkbox when untouched

I want to set the default value of my Angular Checkbox to false (if the Checkbox is untouched).我想将 Angular Checkbox 的默认值设置为 false(如果 Checkbox 未受影响)。 Is there a simple solution for this?有没有一个简单的解决方案?

My checkbox:我的复选框:

<div class="notificationTitle margin-top-2">
    <div class="row">
            <div class="col-md-12">
                    <div>
                            <label class="text-h4 margin-top-2"
                                    for="notifcation"></label>
                            <input
                            [formControlName]="InputField.Notification"
                            [name]="inputField.Notification"
                            id="notifcation"
                            type="checkbox"
                            >
                    </div>
            </div>
    </div>

The simplest way to solve this Issue is to add a If statement in the set(Notification) Method.解决此问题最简单的方法是在 set(Notification) 方法中添加 If 语句。 That way the code checks if the Value is null, if thats true then it changes the Value to false:这样,代码检查值是否为空,如果为真,则将值更改为假:

    public setNotification(notificationInfo: FormNotificationInfo): void {
            if (notificationInfo.notification === null) {
                    notificationInfo.notification = false;
            }
            this.notificationInfo= notificationInfo;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为复选框设置默认值 - How to set a default value for a checkbox 错误:ExpressionChangedAfterItHasBeenCheckedError:以前的值:&#39;ng-untouched:true&#39;。 当前值:&#39;ng-untouched:false&#39; - Error: ExpressionChangedAfterItHasBeenCheckedError: Previous value: 'ng-untouched: true'. Current value: 'ng-untouched: false' 在带有反应形式的角度v4中使用禁用状态时,无法将复选框值设置为false - Unable to set checkbox value to false when using disabled state in angular v4 with reactive forms 在“反应形式”复选框中设置默认值 - Set default value in Reactive Forms checkbox 当过滤器返回false时,RxJs返回旧值 - RxJs Return old value when when filter return false 检查后表达式已更改。 'ng-untouched' 的先前值:'true'。 当前值:“假” - Expression has changed after it was checked. Previous value for 'ng-untouched': 'true'. Current value: 'false' Typescript:无法将默认参数值设置为 false - Typescript : Can't set default parameter value as false 为什么我的表单在页面加载后立即验证所有默认值(未修改的字段)? - Why does my form validate all default value (untouched field) as soon as the page is loaded? 选中时将angular2复选框的值设置为1,将其取消选中以设置为0 - angular2 checkbox value when checked set 1 and unchecked to set 0 Angular 4 填充表单并设置为未触及 - Angular 4 populate form and set to untouched
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM