Angular Radio Button Reactive Form Example

By Hardik Savani May 2, 2024 Category : Angular

Now, let's see post of angular 10 radio button reactive form. you will learn angular 11/10 radio button example. i explained simply step by step angular 10 radio button ng model example. This article goes in detailed on angular 10 radio button example bootstrap. Let's see bellow example radio button in angular 10 reactive form.

we will create reactive form step by step with radio button box using loop. you can use this example with angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17.

Here, i will give you two example to get value of selected radio button by click on submit button and another get checked radio button value in on change value. so we will take "gender" variable and create two radio button. let's see both example one by one.

You need to follow bellow step to add form validation in angular 10.

Example 1: Get Checked Radio Button on Form Submit

Step 1: Import FormsModule

If you want to create form in angular app then you need to import FormsModule from @angular/forms library. so let's add following code to app.module.ts file.

src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({

declarations: [

AppComponent

],

imports: [

BrowserModule,

FormsModule,

ReactiveFormsModule

],

providers: [],

bootstrap: [AppComponent]

})

export class AppModule { }

Step 2: Form with ngModel

In this step, we will write code of html form with ngModel. so add following code to app.component.html file.

I used bootstrap class on this form. if you want to add than then follow this link too: Install Boorstrap 4 to Angular 10.

src/app/app.component.html

<div class="container">

<h1>Angular Radio Button Example - ItSolutionStuff.com</h1>

<form [formGroup]="form" (ngSubmit)="submit()">

<div class="form-group">

<label for="gender">Gender:</label>

<div>

<input id="male" type="radio" value="male" name="gender" formControlName="gender">

<label for="male">Male</label>

</div>

<div>

<input id="female" type="radio" value="female" name="gender" formControlName="gender">

<label for="female">Female</label>

</div>

<div *ngIf="f.gender.touched && f.gender.invalid" class="alert alert-danger">

<div *ngIf="f.gender.errors.required">Name is required.</div>

</div>

</div>

<button class="btn btn-primary" type="submit" [disabled]="!form.valid">Submit</button>

</form>

</div>

Step 3: updated Ts File

In ts file. we will write submit() and get all input fields values. so let's add following code to app.component.ts file.

src/app/app.component.ts

import { Component } from '@angular/core';

import { FormGroup, FormControl, Validators} from '@angular/forms';

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent {

form = new FormGroup({

gender: new FormControl('', Validators.required)

});

get f(){

return this.form.controls;

}

submit(){

console.log(this.form.value);

}

}

Now you can run your application using following command:

ng serve

Example 2: Get Checked Radio Button on Change Event

Step 1: Import FormsModule

If you want to create form in angular app then you need to import FormsModule from @angular/forms library. so let's add following code to app.module.ts file.

src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({

declarations: [

AppComponent

],

imports: [

BrowserModule,

FormsModule,

ReactiveFormsModule

],

providers: [],

bootstrap: [AppComponent]

})

export class AppModule { }

Step 2: Form with ngModel

In this step, we will write code of html form with ngModel. so add following code to app.component.html file.

I used bootstrap class on this form. if you want to add than then follow this link too: Install Boorstrap 4 to Angular 10.

src/app/app.component.html

<div class="container">

<h1>Angular Radio Button Example - ItSolutionStuff.com</h1>

<form [formGroup]="form" (ngSubmit)="submit()">

<div class="form-group">

<label for="gender">Gender:</label>

<div>

<input id="male" type="radio" value="male" name="gender" formControlName="gender" (change)="changeGender($event)">

<label for="male">Male</label>

</div>

<div>

<input id="female" type="radio" value="female" name="gender" formControlName="gender" (change)="changeGender($event)">

<label for="female">Female</label>

</div>

<div *ngIf="f.gender.touched && f.gender.invalid" class="alert alert-danger">

<div *ngIf="f.gender.errors.required">Name is required.</div>

</div>

</div>

<button class="btn btn-primary" type="submit" [disabled]="!form.valid">Submit</button>

</form>

</div>

Step 3: updated Ts File

In ts file. we will write submit() and get all input fields values. so let's add following code to app.component.ts file.

src/app/app.component.ts

import { Component } from '@angular/core';

import { FormGroup, FormControl, Validators} from '@angular/forms';

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent {

form = new FormGroup({

gender: new FormControl('', Validators.required)

});

get f(){

return this.form.controls;

}

submit(){

console.log(this.form.value);

}

changeGender(e) {

console.log(e.target.value);

}

}

Now you can run your application using following command:

ng serve

You can see output:

I hope it can help you...

Shares