How to set default boolean false in formcontrol

Viewed 6935

set default boolean false in formcontrol (primary field) ko

this.contactForm = new FormGroup({
      name: new FormControl('', [ Validators.required]),
      contact: new FormControl('', [ Validators.required,Validators.minLength(10)]),
      email: new FormControl('', [ Validators.required,Validators.email]),
      primary: new FormControl('',[])
    });

HTML File :- please see html file

<form [formGroup]="contactForm">
    <mat-checkbox class="example-margin" formControlName="primary" [value]="primary" [(ngModel)]="primary"> Make this primary </mat-checkbox>
  </form>

console print img

3 Answers

You can use patchValue() in your OnInit() function :

this.contactForm.patchValue({primary: false});

First argument of FormControl gets initial value, so you can easily write:

new FormControl(false, [...]);

Or it could be more complex if you need:

new FormControl({ value: false, disabled: true }, [...]);

You could try this: { email: new FormControl({ value: '', disabled: true }, ...) }

Related