Search code examples
angularcheckboxdefault

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). 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>

Solution

  • The simplest way to solve this Issue is to add a If statement in the set(Notification) Method. 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;
        }