Search code examples
angularangular-materialionic4angular8angular-forms

How to set default boolean false in formcontrol


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


Solution

  • 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 }, [...]);