Deselect Radio Button from Angular Material

Asked

Viewed 1,120 times

0

Hello, I have a problem at Angular. I want my Angular Material button select to be unchecked (uncheck) when the following function is called, but I have no idea how to do it, can anyone help me. I’ll put the codes down here.

Code of the function

uncheckRadio(){
  alert('Botao desmarcado')
}

Code for my Radio Button:

<div  class="form-group col-md6 form-md-checkboxes">
   <label>Instância</label>
   <mat-radio-group (change)="radioChange($event)" formControlName="status">
   <mat-radio-button color="primary" value="primeira">Primeira Instancia</mat-radio-button>
   <mat-radio-button color="primary" value="segunda">Segunda Instancia</mat-radio-button>
   </mat-radio-group>
</div>

1 answer

1


For that just set null in your control.

In the example below I created a button to call the method uncheckRadio

<div  class="form-group col-md6 form-md-checkboxes">
    <label>Instância</label>
    <mat-radio-group (change)="radioChange($event)" formControlName="status">
      <mat-radio-button color="primary" value="primeira">Primeira Instancia</mat-radio-button>
      <mat-radio-button color="primary" value="segunda">Segunda Instancia</mat-radio-button>
    </mat-radio-group>
    <button mat-raised-button class="restore-button" (click)="uncheckRadio()">Restore Defaults</button>
  </div>

In the file ts I take the control in formGroup and reset the value or Seto null.

uncheckRadio() {
   this.formGroup.get('status').reset();
   // Ou pode ser feito assim: this.formGroup.get('status').setValue(null);
}
  • I have the following error: [ts] Property 'formGroup' does not exist on type 'Querysearchdccomponent'.

  • The this formGroup is nothing more than a variable of the type Formgroup that I created in the archive .ts. I did so because I saw in your example code the formControlName="status" so I figured you’re using Reactiveforms in your development. Simply replace the this formGroup for the variable of your Formgroup. Anyway I leave it here in this link the complete example of my code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.