I am creating a radio button with the checked property but it's not showing selected
<mat-radio-group name="radioOpt1" [(ngModel)]="selectedRadio" [ngModelOptions]="{standalone: true}" (change)="radioChange($event)">
<mat-radio-button value="own" checked name="own">Own</mat-radio-button>
<mat-radio-button value="competitor" name="own">competitor</mat-radio-button> </mat-radio-group>
I want the first radio button to be checked by default
Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.
You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .
If using ngModel
then you need to pass value of radio-button to ngModel
.
<mat-radio-group name="radioOpt1" [(ngModel)]="selectedRadio"
[ngModelOptions]="{standalone: true}" (change)="radioChange($event)">
<mat-radio-button value="own" name="own">Own</mat-radio-button>
<mat-radio-button value="competitor" name="own">competitor</mat-radio-button>
</mat-radio-group>
and ts file
selectedRadio = 'own'; //default value
radioChange(e){
console.log(this.selectedRadio)
}
or dynamically populated
<mat-radio-group name="radioOpt1" [(ngModel)]="selectedRadio"
[ngModelOptions]="{standalone: true}" (change)="radioChange($event)">
<mat-radio-button *ngFor="let but of list" [value]="but.id" name="own" >
{{but.name}}
</mat-radio-button>
</mat-radio-group>
ts file
list = [{ "name": "own", id: "own"},{ "name": "competitor", id: "competitor"}];
selectedRadio =this.list[0].id;
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