Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind default value in mat-radio-group angular reactive forms

In my case it needs to active option 01 as default selection. It is working with checked=true property, but value is not binding with the formControlName="options", it is binding when user select any option. if no any user selection options value shows as "null".

  <div class="row">
      <mat-radio-group formControlName="options">
        <mat-radio-button checked=true  value="1">Option 01</mat-radio-button>
        <mat-radio-button  value="2">Option 02</mat-radio-button>
      </mat-radio-group>
    </div>

Please kindly help me to resolve this issue. Thank you.

like image 881
Dimuthu Avatar asked Oct 16 '17 08:10

Dimuthu


People also ask

How to work with angular material radio button?

To work with Angular Material radio button we need to import MatRadioModule in application module. Angular provides MatRadioGroup to group radio buttons. The selector of MatRadioGroup is mat-radio-group. Radio button is created using MatRadioButton and its selector is mat-radio-button.

What is matradiobutton in Angular 2?

MatRadioButton creates radio button enhanced with Material design styling and animations. The selector of MatRadioButton is mat-radio-button that works same as <input type="radio">. All radio buttons with same name creates a set and we can select only one of them.

What is matradiochange in angular?

MatRadioChange is emitted by change event of MatRadioButton and MatRadioGroup. MatRadioChange is used to fetch selected radio button. On this page we will discuss properties of MatRadioGroup and MatRadioButton and their uses, perform radio button validation and will create reactive and template-driven form using Angular Material radio buttons. 1.

How to validate the radio button in ngform?

For validating the Material radio button, we’ll add a class dynamically to the radio button group. The class will be added once, the form is submitted and the radio button value is invalid or not selected. NgForm directive helps in detecting whether the form is submitted. Using the reactive form profileForm you can check whether it’s valid.


4 Answers

What you want to do is to remove the checked and instead set the preselected value to your formControl, so when you build your form:

constructor(private fb: FormBuilder) { 
  this.myForm = this.fb.group({
    options: ['1']
  })
}

and then you just remove the checked attribute:

<mat-radio-group formControlName="options">
  <mat-radio-button value="1">Option 01</mat-radio-button>
  <mat-radio-button  value="2">Option 02</mat-radio-button>
</mat-radio-group>
like image 105
AT82 Avatar answered Oct 18 '22 19:10

AT82


Alternate way:

 this.optionFormGroup = new FormGroup({
  options: new FormControl('1'); // the value type (string) should match
 })

--

  <div class="row" [formGroup]="optionFormGroup">
   <mat-radio-group formControlName="options">
     <mat-radio-button checked value="1">Option 01</mat-radio-button>
     <mat-radio-button  value="2">Option 02</mat-radio-button>
   </mat-radio-group>
  </div>
like image 6
Khuram Niaz Avatar answered Oct 18 '22 19:10

Khuram Niaz


This will default select "yes" option from radio button options.

  <form [formGroup]="optionsFormGroup">
        <mat-grid-list cols="1" rowHeight="75px">
          <mat-grid-tile>
            <label>Option</label>
            <mat-radio-group formControlName="statusOptions" class="m-l-5">
              <mat-radio-button class="m-r-5" value="yes">Yes</mat-radio-button>
              <mat-radio-button class="m-r-5" value="no">No</mat-radio-button>
            </mat-radio-group>
            <mat-icon class="info-icon">help_outline
            </mat-icon>
          </mat-grid-tile>
        </mat-grid-list>
    </form>
initFormControls() {
    this.optionsFormGroup = this.formBuilder.group({
      statusOptions: ['yes'],
    });
  }
like image 1
khizer Avatar answered Oct 18 '22 20:10

khizer


This is look like other answers, but uses boolean values

ts

form: FormGroup = this.formBuilder.group({
  enable: ['']
});

ngOnInit(): void {
  this.form.get('enable').setValue(false);
}

template

<mat-radio-group aria-label="Select an option" formControlName="enable">
   <mat-radio-button [value]="true" color="primary">Enable</mat-radio-button>
   <mat-radio-button [value]="false" color="primary" class="radio">Disable</mat-radio-button>
</mat-radio-group>
like image 1
otaviodecampos Avatar answered Oct 18 '22 19:10

otaviodecampos