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.
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.
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.
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.
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.
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>
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>
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'],
});
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With